• Moving a Range.Find object to another StoryRange (Word 2002 SP-2)

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » Moving a Range.Find object to another StoryRange (Word 2002 SP-2)

    Author
    Topic
    #416760

    The Word MVPs have a macro for replacing a text string wherever it appears in a document (including headers, footers and all other StoryRanges). I’d like to have a more flexible version that accepts a Range.Find object as an optional argument, allowing me to set Style, Font and other properties for the target text and/or replacement text.

    This would be a straightforward task (I think) if I wanted to use a Selection.Find object as the argument (and use the Selection for all the macro’s searches), since the Selection.Find object has “sticky” settings. But to be able to use a Range.Find object as the argument (and search using Range.Find objects, as the MVPs macro does), I need to be able to jump the Range.Find from one RangeStory to another while preserving the other .Find properties (.Text.Style, .Text.Font, etc.).

    It’s easy to move a Range.Find object around within a StoryRangee.g., by using oFind.Parent.SetRange — without losing the .Find object’s other settings. But I don’t know of a way to jump it to a different StoryRange.

    Besides the Selection alternative, I realize I could also write a supplemental MatchFindSettings macro that tediously adjusted all the settings of one Range.Find object to match those of another .Find object. But if there’s a direct way to move a Range.Find object to a different StoryRange, I’d love to hear it.

    Viewing 0 reply threads
    Author
    Replies
    • #933939

      Hi Steve,

      You can write a Find macro that takes a Range as an argument.
      Then you can run that same macro for different Ranges and/or StoryRanges.

      I sometimes use a “helper” macro that runs some given such macro on all StoryRanges:

      Private Sub LoopStoryRanges(myMacro As String)
        Dim myStoryRange As Range
        For Each myStoryRange In ActiveDocument.StoryRanges
          Application.Run myMacro, myStoryRange
          While Not (myStoryRange.NextStoryRange Is Nothing)
            Set myStoryRange = myStoryRange.NextStoryRange
            Application.Run myMacro, myStoryRange
          Wend
        Next myStoryRange
      End Sub
      

      The macro “Sub someMacro(myRange as Range)” itself has a Range as an argument, as described above.
      And a third (simple) macro starts the ball rolling:

      LoopStoryRanges "someMacro"
      

      cheers Klaus

      • #933978

        cheers Thanks for the input. I’m filing your sample away for future use in different circumstances, but at this point I’m leaning against it as a solution here. I implemented an alternate version (using your approach) of a macro that would otherwise call my ReplaceEverywhere macro, and it worked, but it got kind of complicated and cumbersome because of the number of arguments my ReplaceEverywhere macro takes. (You can tell it whether or not to confirm each replacement, or whether to offer a UserForm that lets the user decide whether to confirm or not. You can feed it a Document or not (in which latter case it uses the ActiveDocument). You can feed it a list of Target/Replacer pairs. Etc. Some or all of which arguments (depending on the particular caller) had to be passed from Caller 1 to LoopStoryRanges (which needed to have varg1, varg2, varg3, etc., arguments added) to Caller 2 (the “real” caller) and finally to the one-StoryRange alternative version of ReplaceEverywhere.) And there was another complication or two I won’t bore you (further) with.

        But attempting to implement your suggestion led me to a simpler alternative that I may end up adopting. A macro that would otherwise have fed ReplaceEverywhere a tweaked oFind argument can instead feed it the name of a simple companion macro (say, MacroName_AdjustFind) that accepts a Find object as its only argument and tweaks it appropriately, and ReplaceEverywhere can respond by calling the tweaking macro (using Application.Run) and feeding it each StoryRange’s .Find property before processing that StoryRange.

        Meanwhile…

        I mentioned in my first post that I was trying to avoid the workaround that “tediously adjusted all the settings of one Range.Find object to match those of another .Find object,” but it turned out not to be as tedious as I thought (although it got substantially more tedious after it turned out I couldn’t use the .Font.Duplicate technique). Prior to reading your post, I had ended up addressing the issue by having my ReplaceEverywhere procedure call the attached MatchFindSettings procedure for each StoryRange’s .Find property (matching it to the oFind object passed to ReplaceEverywhere as an argument).

        And so, finally, the Bonus Question: If anyone can explain why using the .Font.Duplicate technique always left me with a .Find object that didn’t find anything, I’m all ears.

    Viewing 0 reply threads
    Reply To: Moving a Range.Find object to another StoryRange (Word 2002 SP-2)

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

    Your information: