Results 1 to 13 of 13
  1. #1
    3 Star Lounger
    Join Date
    Jul 2002
    Location
    Edison, New Jersey, USA
    Posts
    215
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Find/Insert (W97)

    In my documnet, I need to search for a style and insert a "3~" before each entry... and do this for every entry for that style. I'm only able to find the text and replace it, or find, cancel, home, insert... Search, find, etc. But if I do that, I don't know when I've "found them all". I think it has to do with some type of loop, but I'm lost... Help?

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Find/Insert (W97)

    This is a good example of where VBA offers extra flexibility you can't tap by recording a macro. The logic of what you want to do is along these lines (although I haven't tested it).

    Set up the Find parameters, going forward only, stopping at the end.
    This can be copied from a recorded macro
    Execute the Find and, if Found, then...
    You might be able to use:
    Do While Selection.Find.Execute
    Use the .InsertBefore method (I think that's the name, but if not, it's very similar to that) to insert the new text.
    Adjust the selection if necessary.
    You might want to collapse the selection to its end to avoid "re-finding" the same item. Tests will tell...
    Execute again until there's no more to be found.
    If you're using a Do loop, it's simply:
    Loop

    Hope this helps.

  3. #3
    3 Star Lounger
    Join Date
    Jul 2002
    Location
    Edison, New Jersey, USA
    Posts
    215
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Re: Find/Insert (W97)

    Yes, I wrote the following work flow for the process:
    Ctrl Home
    Search for style "XYZ"/ Search Down
    Find Style? No=End of document, Exit Yes=InsertBefore "3~"
    Next Style "XYZ"

    But I'm not figuring out how to code it (I've been playing with it)... in combination of knowing but lil programming, and my VBA crutch books being at home.... <img src=/S/crazy.gif border=0 alt=crazy width=15 height=15>

  4. #4
    3 Star Lounger
    Join Date
    Jul 2002
    Location
    Edison, New Jersey, USA
    Posts
    215
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Re: Find/Insert (W97)

    In my playing ( I have no idea if I'm truly close), I tried: Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("Heading 3")
    With Selection
    .InsertBefore "3~"
    .Collapse direction:=wdCollapseEnd
    End With
    End Sub
    But it doesn't go to the style before it insert... it just does it where the cursor is....


    Option 2, InsertBefore is not allowed.
    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("Heading 3")
    With Selection.Find
    .InsertBefore "3~"
    .Collapse direction:=wdCollapseEnd
    End With


    But it doesn't go to the style before it insert... it just does it where the cursor is....

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Find/Insert (W97)

    Well, if you get bored working on it, try this:

    <pre>Option Explicit

    Sub RecordedFind()
    '
    ' RecordedFind Macro
    ' Macro recorded 12/26/2002 by Jefferson F. Scher
    '
    Selection.HomeKey Unit:=wdStory
    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("Body Text")
    Selection.Find.ParagraphFormat.Borders.Shadow = False
    With Selection.Find
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    End Sub

    Sub FindAndPreface()
    'This part will depend on your needs
    Dim strNewText As String
    strNewText = "3~"
    'Set up the Find using the recorded code,
    ' reorganized slightly
    With Selection.Find
    .ClearFormatting
    .Text = ""
    .Style = ActiveDocument.Styles("Body Text")
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    'Go to the top and search forward in a loop
    With Selection
    .HomeKey Unit:=wdStory
    Do While .Find.Execute
    'A match was found, insert the new text before it
    .InsertBefore strNewText
    'collapse the selection to the end to avoid refinding it
    .Collapse Direction:=wdCollapseEnd
    Loop
    End With
    End Sub</pre>

    The first macro is purely for reference. the .Wrap parameter was changed to wdFindStop to prevent the change from running over and over. In fact, it might not do that, but I didn't even want to risk it. Same with the selection collapse. Just safer to do that even if the macro would run correctly without it...too many weird experiences with Find loops.

  6. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Find/Insert (W97)

    Close: you need to Execute the Find and then insert the new text.

  7. #7
    3 Star Lounger
    Join Date
    Jul 2002
    Location
    Edison, New Jersey, USA
    Posts
    215
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Re: Find/Insert (W97)

    Yahooooo! Worked Awesome, except for one lil (BIG) thing... it kept looping and exited the program to stop it... I don't see why it didn't stop?

  8. #8
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Find/Insert (W97)

    Well... I don't know. Could be something that was "fixed" in Word 2000, so I didn't experience that. You usually can interrupt running VBA code with Ctrl+Break.

    In theory, you could add this to your loop as a failsafe:

    <pre>With Selection
    .HomeKey Unit:=wdStory
    Do While .Find.Execute
    If Left(.Text, Len(strNewText)) = strNewText Then Exit Do
    'A match was found, insert the new text before it
    .InsertBefore strNewText
    'collapse the selection to the end to avoid refinding it
    .Collapse Direction:=wdCollapseEnd
    Loop
    End With</pre>

    But I haven't actually tested it. Does this help?

  9. #9
    3 Star Lounger
    Join Date
    Jul 2002
    Location
    Edison, New Jersey, USA
    Posts
    215
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Re: Find/Insert (W97)

    Hmmm. ....

    When I closed my MSO, I ofcourse hadn't saved prior to running the script, so I had to recopy the script back into the template... now when I run it, I get 'Could not open macro storage runtime error 5981' I didn't get this before. The code is haning up on line:
    .Style = ActiveDocument.Styles("Heading 1") 'and this IS an existing style

  10. #10
    3 Star Lounger
    Join Date
    Jul 2002
    Location
    Edison, New Jersey, USA
    Posts
    215
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Re: Find/Insert (W97)

    Never mind.... I coppied the script again, and it's ok this time.... I must have added a space somewhere... <img src=/S/dizzy.gif border=0 alt=dizzy width=15 height=15>

  11. #11
    3 Star Lounger
    Join Date
    Jul 2002
    Location
    Edison, New Jersey, USA
    Posts
    215
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Re: Find/Insert (W97)

    Here's what the script gave me:

    5.1 Physical Data Model

  12. #12
    3 Star Lounger
    Join Date
    Jul 2002
    Location
    Edison, New Jersey, USA
    Posts
    215
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Re: Find/Insert (W97)

    Ok... discovered something interesting...

    If I'm looking for a 'Heading 1' and the last entry of the document is a 'Heading 1', it continues to loop.
    If the last entry is a "heading 2', then it exits out.

    Hmmmm.

  13. #13
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Find/Insert (W97)

    Okay, that explains it. Yet another of those things that will trip up the hasty macro author...one can never get "past" the last paragraph by collapsing a selection that includes it. To create an escape valve for that, you'd need to do something like:

    If .Start = ActiveDocument.Content.End - 1 Then Exit Do

    This will identify a find result that is limited to the final paragraph mark (i.e, Selection.Start is the second-to-last character in the body of the document).

    Or, you could just be very careful what document you run it in. <img src=/S/wink.gif border=0 alt=wink width=15 height=15>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •