Results 1 to 3 of 3
  1. #1
    Lounger
    Join Date
    Dec 2001
    Location
    Santa Barbara, California, USA
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Find and replace in VBA (Word 2000/2002)

    Hello all,
    I'm attempting to do a find and replace in Word 2000 and 2002 with VBA. Trying to find such things as "(128L2 cache)", (256L2 cache) using wildcards and change the formatting of the text that is found. So far I have been unable to figure out how to do this and leave the original text with only the formatting changed. It's either deletes the found text or replaces it with text from a previous search and replace, despite my attemps to clear the previous replacement text setting.

    What's the proper syntax to use for this?

    Some sample code I've been using to experiment. Also, I've noticed that if I record a search-and-replace Macro, the results differ when I run the macro.

    Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
    .Text = "([0-9]{1,4})"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchAllWordForms = False
    .MatchSoundsLike = False
    .MatchWildcards = True
    End With
    Selection.Find.Execute Replace:=wdReplaceAll


    Thanks,
    Steve

  2. #2
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find and replace in VBA (Word 2000/2002)

    Hi Steve:

    I'm not a VBA expert, but I can tell you a couple of things:
    1. Your Find expression won't find what you want, assuming you want to find expressions like those within your quotes.
    2. In Word 2000, if you include a fomatting requirement in both the Find & Replace box, Word will change the formatting. If you either leave out the formatting in either or both, it will delete what it finds. That's what you're asking it to do when you leave the Replace box empty.
    3. In order to keep all the text that you find, you must put ^& in the replace box.

    e.g.
    If you were replacing your above expressions in normal text with hyperlink character style, your macro would look something like this:<pre> Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("Normal,n")
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Replacement.Style = ActiveDocument.Styles("Hyperlink")
    With Selection.Find
    .Text = "([0-9]{3}[A-z][0-9] cache)"
    .Replacement.Text = "^&"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchAllWordForms = False
    .MatchWildcards = True
    End With
    Selection.Find.Execute Replace:=wdReplaceAll</pre>

    Hope this helps,

  3. #3
    Lounger
    Join Date
    Dec 2001
    Location
    Santa Barbara, California, USA
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find and replace in VBA (Word 2000/2002)

    Thank you, that's just what I need. Of course, now that I've got my answer, I just relooked at the help file and there is the ^& which I always missed seeing somehow before.

    Steve

Posting Permissions

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