Access db, find and replace in word

J

Jon Petitta

In a form that I created in Access I'm pulling data from
tables and running a find and replace in word. I was
developing on one machine and it worked fine. Went to
another machine, and it gave me an invalid procedure
call. I checked the ver's in word and the one that works
is 10.4219.4219, the one that doesn't is 10.5522.4219. I
updated the other machine and now they both don't work.
It does everything else, it opens word, opens the doc, but
stops on the find and gives me an error. If I take out
the find it works fine. The code works as a macro when
running local to word, but when I try to run from access
it gives me the error. Here is the code in Access VBA

Private Sub word_Click()
Dim intLBIndex As Integer
On Error GoTo Err_word_Click

intLBIndex = Me.lbQuote.ListIndex
strSStreet = Me.lbQuote.Column(1, intLBIndex)
strSCity = Me.lbQuote.Column(2, intLBIndex)
strSState = Me.lbQuote.Column(3, intLBIndex)
strSZip = Me.lbQuote.Column(4, intLBIndex)

Set oApp = New word.Application
oApp.Visible = True
oApp.Documents.Open ("d:\project\complete set of
forms\Remediation Agreement Contract v3.1.doc")

With oApp.Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "%ClientName"
.Replacement.Text = strClientName
.Execute Replace:=wdReplaceAll
.ClearFormatting
.Replacement.ClearFormatting
.Text = "%SiteSTreet"
.Replacement.Text = strSStreet
.Execute Replace:=wdReplaceAll
.ClearFormatting
.Replacement.ClearFormatting
.Text = "%SiteCity"
.Replacement.Text = strSCity
.Execute Replace:=wdReplaceAll
.ClearFormatting
.Replacement.ClearFormatting
.Text = "%SiteState"
.Replacement.Text = strSState
.Execute Replace:=wdReplaceAll
.ClearFormatting
.Replacement.ClearFormatting
.Text = "%SiteZip"
.Replacement.Text = strSZip
.Execute Replace:=wdReplaceAll
End With

oApp.ActiveWindow.ActivePane.View = wdPrintView
oApp.ActiveWindow.ActivePane.View.SeekView =
wdSeekCurrentPageFooter
oApp.ActiveWindow.ActivePane.View.NextHeaderFooter

With oApp.Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "%ClientName"
.Replacement.Text = strClientName
.Execute Replace:=wdReplaceAll
End With


oApp.ActiveDocument.SaveAs ("d:\project\complete set
of forms\booya.doc")
oApp.Application.Quit
Exit_word_Click:
Exit Sub

Err_word_Click:
MsgBox Err.Description
Resume Exit_word_Click

End Sub

Any help would be greatly appreciated
Jon
 
D

Doug Robbins - Word MVP

Hi Jon,

Instead of using find and replace, I would insert DOCVARIABLE fields in the
locations where you have each %Dataitem in the document/template and use
code to set the values for each document variable and then update the fields
in the document.

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 

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