• Find/Insert (W97)

    Author
    Topic
    #381092

    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?

    Viewing 0 reply threads
    Author
    Replies
    • #640805

      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.

      • #640813

        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…. crazy

        • #640816

          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….

          • #640818

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

        • #640817

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

          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

          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.

          • #640820

            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?

            • #640821

              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:

              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

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

            • #640822

              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

            • #640823

              Never mind…. I coppied the script again, and it’s ok this time…. I must have added a space somewhere… dizzy

            • #640826

              Here’s what the script gave me:

              5.1 Physical Data Model

            • #640830

              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.

            • #640854

              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. wink

    Viewing 0 reply threads
    Reply To: Find/Insert (W97)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: