Results 1 to 3 of 3
  1. #1
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

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

    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 StoryRange -- e.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.

  2. #2
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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:<pre>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
    </pre>

    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:<pre>LoopStoryRanges "someMacro"
    </pre>

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16> Klaus

  3. #3
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

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

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16> 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.
    Attached Files Attached Files

Posting Permissions

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