Replace action in Word VBA

N

Nithya

I am totally new to VB. So I thought I should post my
question here. This is part of my code.

Set dbLocal = CurrentDb()
strCurrAppDir = Left$(dbLocal.Name, InStr
(dbLocal.Name, "\ECMS old.mdb"))
strFinalDoc = strCurrAppDir & "DemoTest.doc"

On Error Resume Next
Kill strFinalDoc

On Error GoTo Err_Generate_Letter_Click
FileCopy strCurrAppDir & "\sample.DOT", strFinalDoc

Set appWord = CreateObject("Word.Application")
Set wordDoc = appWord.Documents.Add(strFinalDoc)

appWord.Visible = True
Set replaceCodes = dbLocal.OpenRecordset
("StateReplaceCodes")

Do While Not replaceCodes.EOF

varReplaceWith = Eval(replaceCodes!ReplaceWithText)
varReplaceWith = IIf(IsNull(varReplaceWith), " ", CStr
(varReplaceWith))

wordDoc.Content.Find.ClearFormatting
wordDoc.Content.Find.Replacement.ClearFormatting
With wordDoc.Content.Find
.Text = replaceCodes!Code
MsgBox (varReplaceWith)
.Replacement.Text = varReplaceWith
.Format = True
End With
wordDoc.Content.Find.Execute Replace:=wdReplaceAll

replaceCodes.MoveNext
Loop

replaceCodes.Close

Exit_Generate_Letter_Click:
Exit Sub

Err_Generate_Letter_Click:
MsgBox Err.Description
Resume Exit_Generate_Letter_Click

End Sub

Here StateReplaceCodes is my table in Access 2002 with 2
fields Code and ReplaceWithText. I have a template in Word
(sample.DOT) and I want the new document (TestDemo.doc) to
be replaced with the new values. I have also included a
message box to see if I get all the values. Everything
seems to work fine. The text is found and it also picks up
the replacing text. But the problem arises only when
replacing I think. Can you please help me in this.
Thanks in Advance,
Nithya
 
N

Nithya

One small correction, the DemoTest.doc is just a copy of
the template. All that works fine. But the new document
that opens up does not contain the replaced values.
Thanks,
Nithya
 

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

Similar Threads


Top