Results 1 to 14 of 14
  1. #1
    2 Star Lounger
    Join Date
    Dec 2002
    Location
    San Francisco, California, USA
    Posts
    108
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Seach/Replace multiple terms (Word 2003)

    I need to write a macro that will search a given block of text looking for any one maybe 30 different strings. The block will contain, at most, just one of those strings. If the macro finds one of the strings, it needs to replace the string with a different one. There will be a single, unique replacement string for each of the 30 search strings.

    I know I can just do a series of 20 little find/replace code snippets, each one with one of the 30 find/replace pairs. But, that doesn't seem like a very nice way of doing this. Also, it would be difficult to maintain, if for example my needs change later on and I need to do something to the search text, if found, besides just replacing it. So I'm thinking there must be a way to use some sort of way to do what I want using a loop, and some string functions, rather than doing serial find/replaces on a selection. But I've pretty much exhausted my knowledge of VBA at that point.

    Does anyone have any ideas regarding how to do this?

    thanks.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Seach/Replace multiple terms (Word 2003)

    You can write a generic Find/Replace procedure that takes two arguments, the find text and the replacement text:

    Sub MyReplace(strFindWhat As String, strReplaceWith As String)
    ...
    End Sub

    and call it 30 times from a macro:

    Sub MultiReplace()
    MyReplace "this", "that"
    MyReplace "something", "else"
    ...
    End Sub

    or create an array and loop through it:

    Sub MultiReplace
    Dim strArray(1 To 30, 1 To 2) As String
    Dim i As Integer

    strArray(1, 1) = "this"
    strArray(1, 2) = "that"
    strArray(2, 1) = "something"
    strArray(2, 1) = "else"
    ...

    For i = 1 To 30
    MyReplace stArray(i, 1), strArray(i, 2)
    Next i
    End Sub

  3. #3
    2 Star Lounger
    Join Date
    Dec 2002
    Location
    San Francisco, California, USA
    Posts
    108
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Seach/Replace multiple terms (Word 2003)

    Thanks. Any idea how do I declare a two-dimensional DYNAMIC array? I like the array idea, but since I'll be changing the number of search/replace terms, I figure a dynamic array will be better. But if I just do a Dim strArray() As String, when I try to populate it in two dimensions (e.g., strArray(1,1) = "text"), I get a "subscript out of range" error. I did a google search on "two dimensional dynamic array" but can't find any code for VBA.

  4. #4
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Seach/Replace multiple terms (Word 2003)

    When I needed to do something like this I just used two 1-dimensional arrays.

    StuartR

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Seach/Replace multiple terms (Word 2003)

    If you declare a dynamic array, you must always ReDim it with specific dimensions before using it.

    Dim strArray() As String
    Dim n As Long
    ...
    n = 37
    ReDim strArray(1 To n, 1 To 2)
    ...
    For i = 1 To n
    ...
    Next i

  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: Seach/Replace multiple terms (Word 2003)

    If these are strings with no direct formatting to be preserved, you also can look into using regular expressions. Andrew77 from O'Reilly has posted a few code samples that might help get you started.

  7. #7
    2 Star Lounger
    Join Date
    Dec 2002
    Location
    San Francisco, California, USA
    Posts
    108
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Seach/Replace multiple terms (Word 2003)

    I think I've got it using Hans's array thing, now that I understand what a dynamic array is not. There is one other find/replace issue that's still bugging me, though:

    In some of the blocks I've got, the find text is all caps; in others it's regular title case. So, to search for all the text, I've got to set MatchCase to false. Of course, that means that the replacement text will match the case of whatever case the find text actually is in, whereas I want th ereplacement text to be all in title case, regardless of the case of the find text. So, I've got some code that converts the replacement text to title case after I've done the replacement, so the relevant part of the macro looks like this:

    [FindText and ReplaceText set earlier in macro using Hans's array idea]
    With Selection.Find
    .Text = FindText
    .MatchCase = False
    .Replacement.Text = ReplaceText
    End With
    Selection.Find.Execute
    If Selection.Find.Found = True Then
    Selection.Range.Case = wdTitleCase
    Selection.TypeText (ReplaceText)
    End If

    You'll note that this code is bizarre (at least it is to me). Rather than using the Replace function, it just finds the find text, and then uses TypeText to type over it with the replacement text in the proper case. That's because I can't figure out how to use the the Replace function and force the correct case. Am I missing something, or is this really what word forces me to do?

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Seach/Replace multiple terms (Word 2003)

    If you specify the ReplaceText in lower case, Word will adjust it to the capitalization of the text being replaced automatically: lower case replaced by lower case, upper case replaced by upper case, title case replaced by title case. (MatchCase must be False).

  9. #9
    2 Star Lounger
    Join Date
    Dec 2002
    Location
    San Francisco, California, USA
    Posts
    108
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Seach/Replace multiple terms (Word 2003)

    I guess I don't understand you. I tried a test doc with just one word in it: "TEST" (all caps). Then I excuted the following code, where the replacement text is specfied in all lower case ('newtest").. When run, the code replaces "TEST" with "NEWTEST". That is, the replacment text follows the case of the text that's found, regardless of how the replacement text is specified, and with MatchCase set to False. What am I missing? (BTW, how do you your messages to maintain their indentation? When I post my messages, they lose all the formatting in the code snippets.)

    Sub test()

    Selection.WholeStory

    With Selection.Find
    . MatchCase = False
    .Text = "test"
    .Replacement.Text = "newtest"
    End With

    Selection.Find.Execute Replace:=wdReplaceAll

    End Sub

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Seach/Replace multiple terms (Word 2003)

    You describe the same behavior as I did: when replacing with MatchCase = False, "the replacement text follows the case of the text that's found". In my opinion, that's reasonable and desirable behavior in most situations. If your document had contained "Test", it would have been replaced with "Newtest", and if it had contained "test", it would have been replaced with "newtest".

    Of course, you can set MatchCase to True, and specify Newtest as replace text. In that case, "Test" and "TEST" would not be found, but "test" would be replaced with "Newtest".

    If you don't like this behavior, then you can't use Word's Replace feature, so you will have to roll your own.

  11. #11
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Seach/Replace multiple terms (Word 2003)

    HTML ignores multiple spaces in the source of a web page.

    I use <!t>[tab]<!/t> tags for the indentation. An alternative is to put a <!t>[pre]<!/t> tag before, and a <!t>[/pre]<!/t> tag after a code fragment. All text between these tags is displayed "as is" in a fixed width font. See <!help=19>Help 19<!/help> for details.

  12. #12
    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: Seach/Replace multiple terms (Word 2003)

    You might want to try wildcard searching, as illustrated in the attached.

  13. #13
    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: Seach/Replace multiple terms (Word 2003)

    Some days I can't keep my hands off other people's code. (Just ignore me and I generally go away.) A little streamlining, if you wish:
    <pre> With Selection.Find
    .Text = FindText
    .MatchCase = False
    If .Execute = True Then
    Selection.TypeText ReplaceText
    End If
    End With</pre>


  14. #14
    2 Star Lounger
    Join Date
    Dec 2002
    Location
    San Francisco, California, USA
    Posts
    108
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Seach/Replace multiple terms (Word 2003)

    Thanks. That is *extremely* helpful. I guess it pays to read the entire help file <g>.

    And thanks to everyone for the suggestions. I've got the macro up; and working, and it's even readable. Now, if I only can figure out how I managed to corrupt my normal.dot while I was editing the macro, .... (Fortunately, there's a backup.)

Posting Permissions

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