Find/replace in a Word document programmatically?

R

robeito

Hi everybody
I have an Access project with the following code in a form:

Public Sub saleAword()
Dim oWord As Object
Dim oWdoc As Object

On Error Resume Next
Set oWord = GetObject(, "Word.Application")
If Err.Number = 429 Then Set oWord = CreateObject("Word.Application")
On Error Goto 0

Set oWdoc = oWord.Documents.Add(CurrentProject.Path &
"\miDocWordOriginal.doc")

oWdoc.content.Find.Execute "#año#", False, , , , , , , , Nz(Me.Año),
wdReplaceAll

oWdoc.content.Find.Execute "#objetivo#", False, , , , , , , ,
Nz(Me.Objetivo), wdReplaceAll
....
....

I'm trying to replace every occurrence of the word "#año#" and
"#objetivo#" by the corresponding current values in the form.

The code works fine for "#año#" , but fails replacing "#objetivo#" because
that field has over 500 characters long

Is there another way to do this?

Thanks in advance!!

By the way, oWdoc.content.Find.Execute is not working in headers :(
 
H

Helmut Weber

hmm...
I see now,
the question was not about finding a long string,
but finding a short string and replacing it by a long one.

About that:

Sub Test699()
Dim rTmp As Range
Dim sLng As String
sLng = String(600, "x")
Set rTmp = ActiveDocument.Range
With rTmp.Find
.Text = "Hallo"
While .Execute
rTmp.Text = sLng
rTmp.Collapse
Wend
End With
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 

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