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
angus@NOSPAMiteloffice.com
<Remove NOSPAM to email me>

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

Rick
Thu Dec 04 17:53:16 CST 2003

Hi Angus,

If your string variable contains text, probably the find
is not succeeding. So, try looking at the
http://www.mvps.org/word/FAQs/MacrosVBA/FlushFR.htm
article. Also, you should specify either the wdReplaceOne
or wdReplaceAll constant for the Replace parameter instead
of True.

Rick

>-----Original Message-----
>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
>angus@NOSPAMiteloffice.com
><Remove NOSPAM to email me>
>
>
>.
>

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

Angus
Fri Dec 05 05:37:23 CST 2003

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




"Rick Stebbins" <AutomateOffice@yahoo.com> wrote in message
news:d47201c3bac1$c98b9b50$a601280a@phx.gbl...
> Hi Angus,
>
> If your string variable contains text, probably the find
> is not succeeding. So, try looking at the
> http://www.mvps.org/word/FAQs/MacrosVBA/FlushFR.htm
> article. Also, you should specify either the wdReplaceOne
> or wdReplaceAll constant for the Replace parameter instead
> of True.
>
> Rick
>
> >-----Original Message-----
> >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
> >angus@NOSPAMiteloffice.com
> ><Remove NOSPAM to email me>
> >
> >
> >.
> >



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

Rick
Fri Dec 05 18:26:07 CST 2003

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


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

Cindy
Sat Dec 06 04:46:02 CST 2003

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
:-)


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

Angus
Sat Dec 06 11:01:59 CST 2003

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



"Cindy M -WordMVP-" <C.Meister-C@hispeed.ch> wrote in message
news:VA.00008dbb.00784984@speedy...
> 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
> :-)
>



Re: Word VBA code with Windows XP and Word 2002 works for my test PC but not customer system - Found solution!!! by Angus

Angus
Sat Dec 06 17:28:11 CST 2003

Hello

http://support.microsoft.com/default.aspx?scid=kb;EN-US;292744

explains the problem. It is a clash with an old Excel type library which
apparently causes the problem!

By using late binding on the object which does the Find you get round the
problem. But not ideal.

Angus



"Angus Comber" <ac@iteloffice.com.PLEASENOSPAM> wrote in message
news:#vO7#rBvDHA.640@TK2MSFTNGP11.phx.gbl...
> 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
>
>
>
> "Cindy M -WordMVP-" <C.Meister-C@hispeed.ch> wrote in message
> news:VA.00008dbb.00784984@speedy...
> > 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
> > :-)
> >
>
>



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

Jay
Sat Dec 06 19:44:49 CST 2003

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.

"Angus Comber" <ac@iteloffice.com.PLEASENOSPAM> wrote:

>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
>
>
>
>"Cindy M -WordMVP-" <C.Meister-C@hispeed.ch> wrote in message
>news:VA.00008dbb.00784984@speedy...
>> 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
>> :-)
>>
>


--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://www.mvps.org/word

Re: Word VBA code with Windows XP and Word 2002 works for my test PC but not customer system - Found solution!!! by Cindy

Cindy
Sun Dec 07 02:38:57 CST 2003

Hi Angus,

> http://support.microsoft.com/default.aspx?scid=kb;EN-US;292744
>
> explains the problem. It is a clash with an old Excel type library which
> apparently causes the problem!
>
Glad you found it :-)

Cindy