Problems with this VBA code - please help

A

Angus Comber

The code below opens a new Word doc based on a template. It generally works
fine but for some PC's delivered by Dell where they pre-installed Office XP
Professional (Word 2002) the Find code doesn't work.

Anyone any ideas why the Find would not work - the Find works on my test
PCs - including a test PC running Windows XP and Office XP Professional.
But not on these Dell delivered PC's. I can't understand why?


Code:

Dim objWord As Word.Application
Dim objWordDoc As Word.Document
Dim docspath As String ' Path to template [Template has <Address> and
<Dear> in document]
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
End If
If objWord Is Nothing Then
MsgBox "objWord is nothing"
Exit Sub
End If
objWord.Visible = True

' To run this code you will need to change this path to the path you save
letter.dot
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"
Exit Sub
End If

Dim bRet As Boolean

objWordDoc.Range.Find.ClearFormatting
objWordDoc.Range.Find.Replacement.ClearFormatting
objWordDoc.Range.WholeStory

' All the Find code lines below do NOT work on Dell delivered PC's
bRet = objWordDoc.Range.Find.Execute("<Address>", False, False, False,
False, False, True, Word.wdFindContinue, False, StrToWord,
Word.wdReplaceAll)
MsgBox bRet & " returned from objWordDoc.Range.Find.Execute ..." ' returns
False with Dell Office XP Pro pre-installed PC's
bRet = objWord.Selection.Find.Execute(findtext:="<Dear>",
replacewith:="FormattedDear", Replace:=wdReplaceAll)
MsgBox bRet & " returned from objWord.Selection.Find.Execute ..."
bRet = objWordDoc.Range.Find.Execute("<Dear>", False, False, , , , , , ,
sDear, True)
MsgBox bRet & " returned from objWordDoc.Range.Find.Execute ..."


MsgBox "Just to show you that other Word automation commands work OK, Next
we will type in some text"
' TypeText works fine ALWAYS!
objWord.Selection.TypeText "This is some text here I have typed in!!"

I would really appreciate some help on this.

Angus Comber
(e-mail address removed)
 
W

Word Heretic

G'day "Angus Comber" <[email protected]>,

you haven't set the default formatting. Try explicitly setting each
Find property.


The code below opens a new Word doc based on a template. It generally works
fine but for some PC's delivered by Dell where they pre-installed Office XP
Professional (Word 2002) the Find code doesn't work.

Anyone any ideas why the Find would not work - the Find works on my test
PCs - including a test PC running Windows XP and Office XP Professional.
But not on these Dell delivered PC's. I can't understand why?


Code:

Dim objWord As Word.Application
Dim objWordDoc As Word.Document
Dim docspath As String ' Path to template [Template has <Address> and
<Dear> in document]
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
End If
If objWord Is Nothing Then
MsgBox "objWord is nothing"
Exit Sub
End If
objWord.Visible = True

' To run this code you will need to change this path to the path you save
letter.dot
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"
Exit Sub
End If

Dim bRet As Boolean

objWordDoc.Range.Find.ClearFormatting
objWordDoc.Range.Find.Replacement.ClearFormatting
objWordDoc.Range.WholeStory

' All the Find code lines below do NOT work on Dell delivered PC's
bRet = objWordDoc.Range.Find.Execute("<Address>", False, False, False,
False, False, True, Word.wdFindContinue, False, StrToWord,
Word.wdReplaceAll)
MsgBox bRet & " returned from objWordDoc.Range.Find.Execute ..." ' returns
False with Dell Office XP Pro pre-installed PC's
bRet = objWord.Selection.Find.Execute(findtext:="<Dear>",
replacewith:="FormattedDear", Replace:=wdReplaceAll)
MsgBox bRet & " returned from objWord.Selection.Find.Execute ..."
bRet = objWordDoc.Range.Find.Execute("<Dear>", False, False, , , , , , ,
sDear, True)
MsgBox bRet & " returned from objWordDoc.Range.Find.Execute ..."


MsgBox "Just to show you that other Word automation commands work OK, Next
we will type in some text"
' TypeText works fine ALWAYS!
objWord.Selection.TypeText "This is some text here I have typed in!!"

I would really appreciate some help on this.

Angus Comber
(e-mail address removed)

Steve Hudson

Word Heretic, Sydney, Australia
Tricky stuff with Word or words for you.
wordheretic.com

If my answers r 2 terse, ask again or hassle an MVP,
at least they get recognition for it then.
Lengthy replies offlist require payment.
 

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