Avatar of CThomp2005
CThomp2005

asked on 

Word 2003 VBA - Simple Find/Replace text

Hello.  Thought this would be easy, but am having problems getting this to do *anything*.  Goal is to Find/Replace the text "2007" in a Word doc and replace with "2008" (replace all) using VBA.  Eventually, this small routine will be embedded in a shell I've created that opens all Word docs in a specified folder, does the find/replace, then closes the doc, saving the changes.

The "shell" works fine, but the Find/Replace doesn't seem to function.  I recorded the Find/Replace routine and pulled out the relavent code (below):

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "2007"
        .Replacement.Text = "2008"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

During the recording, the Find/Replace found the two instances of 2007 and changed them without a problem.  So doing it manually, it works perfectly.  One instance is in a text box, one is in a section header.

Running just the snippet above on an already-open doc does nothing.  I'm obviously missing something - some way that Find/Replace running in VBA differs from the manual Find/Replace done within the open doc.

Am pretty familiar with VBA for Excel and Access, but not much so in Word.  Any help would be greatly appreciated!
Microsoft Word

Avatar of undefined
Last Comment
GrahamSkan

8/22/2022 - Mon