Find and Replace problems with Word OOM and VB6

B

Bryan Dickerson

Hi all. Newbie to the group. Any help is greatly appreciated.

I have a VB6 routine (a method in a class) that customizes a Word document
for
attaching to an email and sending out. Recently our marketing folks wanted
to get a bit more fancy with the document and asked that I put some 'custom'
data values in the header of the document. So in addition to the general
content of the document being customized, now there are 'tags' (for lack of
a better term) in the header that my routine uses Find and Replace to
customize to the user. The following is a portion of the code that I
thought I had tested and debugged. This morning when I rolled it out, I
don't think the Force was with me as now it's not working.
-----------------------------------------------
Dim oWord As Word.Application
Dim oWDoc As Word.Document

Set oWord = New Word.Application
Set oWDoc = oWord.Documents.Open("C:\Template.doc")

With oWord.Selection.Find
.Wrap = wdFindContinue
.Forward = True
.Text = "UserName"
.Replacement.Text = "Joe Blow"
.Execute Replace:=wdReplaceAll
End With
-----------------------------------------------
The text "UserName" is in my Template document in the header, but when this
code runs, it replaces all occurences of "UserName" that appear in the body
of the document, but not the one that is in the header. To try and figure
this out, I went in to see what code Word VBA would generate if I recorded
the actions of Finding and Replacing the text, which, by the way, did find
and replace all occurences of "UserName", including those in the header.
This is the code that the macro recorder generated:
----------------------------------------------
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "UserName"
.Replacement.Text = "Joe Blow"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
 
B

Bryan Dickerson

That worked!! Stories, huh? I've seen that terminology before but I have
never had the occasion or need to work with them until now. But whatever
works!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top