Word VBA code with Windows XP and Word 2002 works for my test PC but not customer system

A

Angus Comber

I have written a VB6 program which generates a Word letter based on a
template and then runs the command below:

objWord.Selection.Find.Execute "<Address>", , , , , , , , , strToWord, True

where objWord is of type Word.Application

This works fine on Office 97, 2000 and on my Word 2002 on XP test PC.

It is funny because most of the VBA code works - ie the code to load a Word
document based on the correct template works fine. The problem is that the
<Address> text is NOT replaced with the contents of the strToWord variable.

Anyone any ideas why?

Angus Comber
(e-mail address removed)
<Remove NOSPAM to email me>
 
A

Angus Comber

Unfortunately the article didn't really help

Here is my code

Private Sub cmdMCA_Click()
Dim objWord As Word.Application
Dim objWordDoc As Word.Document '''''''''''''''''****
Dim docspath As String
Dim StrToWord As String
StrToWord = "Angus Comber 1 The Avenue Berks"
Dim sDear As String
sDear = "Angus"
On Error Resume Next 'WordStart
Set objWord = GetObject(, "Word.Application")
If Err.Number = 429 Then
Set objWord = New Word.Application '
CreateObject("Word.Application")
End If
If objWord Is Nothing Then
MsgBox "objWord is nothing"
Exit Sub
End If
objWord.Visible = True

docspath = "c:\program files\ioffice\ltemplat\letter.dot"

Set objWordDoc = objWord.Documents.Add(docspath, False)
' Sometime get a valid objWord object but problem creating document based on
template.
' So still need to check a valid objWordDoc created
If objWordDoc Is Nothing Then
MsgBox "Unable to create Word Document object based on template: " &
docspath & " Unable to proceed with Word creation", vbCritical, "Word Error"
' LogError Err.Number, "Unable to create objWordDoc object. 'Set
objWordDoc = objWord.Documents.Add(docspath, Fales)'", "Letter in
frmContacts"
Exit Sub
End If

Dim bRet As Boolean
bRet = objWordDoc.Range.Find.Execute("<Address>", , , , , , , , , StrToWord,
True)
MsgBox "Return value from Execute Address: " & bRet

bRet = objWordDoc.Range.Find.Execute("<Dear>", False, False, , , , , , ,
sDear, True)
MsgBox "Return value from Execute Dear: " & bRet

''''''''' MIGHT WORK -> '''''''
objWordDoc.Range.WholeStory ' select all text
objWord.StatusBar = "objWordDoc.Range.WholeStory"
bRet = objWordDoc.Range.Find.Execute("<Address>", False, False, False,
False, False, True, Word.wdFindContinue, False, "Abba", Word.wdReplaceAll)
MsgBox "Return value from Execute Address: " & bRet
Dim myrange As Word.Range
Set myrange = ActiveDocument.Content
myrange.Find.Execute FindText:="Add", ReplaceWith:="hello",
Replace:=wdReplaceAll

End Sub

All msgbox's return false.

The machine was installed by Dell - apparently their default Office install.
If I look in Add/Remove Programs on the PC I see Microsoft Office XP
Professional (Size 21.53MB)

I can create a normal macro in Word doing similar thing and it works.

The VB program loads the Word doc OK - so automation is working - just not
the Find.Execute line!

I am desperate to get this fixed asap.

Any help would be much appreciated.

Angus
 
R

Rick Stebbins

I usually select the document's contents and perform the
Replace All using the Selection object, since it seems to
run faster than Range. I also specify all parameters and
flush out any find formatting before every Find.

With WordObject.Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
End With
WordObject.Selection.Find.Execute "FindText", 0, 0, 0, _
0, 0, True, wdFindStop, 0, "ReplacementText", _
wdReplaceAll
 
C

Cindy M -WordMVP-

Hi Angus,
On Error Resume Next 'WordStart
This is probably at the root of why nothing is getting
replaced. Remove it and see what kind of error message
you're getting.

*On Error Resume Next should NEVER EVER be used without On
Error GoTo 0* following within a couple of lines. NEVER!

I'm betting that what you're running into is a known
problem automating Word's FIND functionality on systems
with a particular configuration. Once you get the error
message, look it up in the Knowledge Base on the microsoft
website in connection with the terms wd2000 and Find.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update
Sep 30 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any
follow question or reply in the newsgroup and not by e-mail
:)
 
A

Angus Comber

On removing on error resume next for the Find work this is what happened


objWordDoc.Content.WholeStory
objWordDoc.Content.Find.ClearFormatting
objWordDoc.Content.Find.Replacement.ClearFormatting ' runtime error
"-1073741819 (c0000005)' Method '~' of object '~' failed
bRet = objWordDoc.Content.Find.Execute("<Address>", False, False, False, _
False, False, True, Word.wdFindContinue, 0, "ReplacementText", _
Word.wdReplaceAll)

However, if I remove the Find.Replacement.ClearFormatting line, the Execute
line still doesn't replace the text!

However, as you say there may be some clue there if
Find.Replacement.ClearFormatting causes the runtime error. I have had a
look on the web and cannot find anything to help me.

Angus
 
J

Jay Freedman

Hi, Angus,

The samples you've shown in your posts have used the
objWordDoc.Content.Find object and the objWordDoc.Range.Find object,
and neither of them should work in *any* version of Word VBA.

The problem is that when the text is found, the .Execute method is
supposed to relocate the object (Range or Selection) to cover just the
found text. But objWordDoc.Content and objWordDoc.Range are both
defined to point permanently to the *whole* document and can't be
relocated.

Instead, either use the .Find member of objWord.Selection, or use a
Range object that you prepare this way:

Dim oRng As Word.Range
Set oRng = objWordDoc.Range

Either of these objects can be relocated as needed.
 

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