Word 2003 Find/Change Automation from Excel 2003 crashes Excel 200

G

Guest

I just upgraded to Office Professional 2003 from Office Professional XP.

The following code, when executed, completely and irrevocably crashes
Microsoft Excel 2003 at the line <.Text = "^pDescription">. It worked just
find in Microsoft Excel XP. It even asks to send a report to Microsoft. Every
time. Easily reproducible. I have updated to all service packs/patches, etc.,
too.

Also, the line <Selection.Find.Replacement.ClearFormatting> gives a
"Automation error: the procedure number is out of range" error, which it
didn't do before in Excel 2003.

What the heck happened between the two versions? Now my code has completely
blown up!!! Any ideas????? Any help would be greatly appreciated, as we have
to get this program up and running for production! Thanks!!!

Public Sub Test()
Dim appWord As Word.Application

Set appWord = New Word.Application
appWord.Documents.Add

appWord.Selection.Find.ClearFormatting
appWord.Selection.Find.Replacement.ClearFormatting
With appWord.Selection.Find
.Text = "^pBREAK"
.Replacement.Text = "-----"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
appwordSelection.Find.Execute Replace:=wdReplaceAll

appWord.ActiveDocument.SaveAs "C:\TEST.DOC"
appWord.ActiveDocument.Close
appWord.Quit

Set appWord = Nothing

End Sub
 
T

Tom Ogilvy

There is no line

<.Text = "^pDescription">

regardless,
since all of this is performing actions in word, why not ask in a word group
where you may have a higher probability of reaching someone who knows about
the word object model.
 
G

Guest

Thanks, Tom.

You're right. The line is the "^pBREAK" line. I was testing it again with a
different text to find. So, the line that is causing the crash is the ".Text
= ^pBREAK" line.

The reason I posted it here is because it works flawlessly in Word 2003.
It's when you call it from a Word.Application object from within Excel that
it crashes. So, it's not necessarily a Word problem. Make sense?
 
T

Tom Ogilvy

Did it work the first time you ran it? Then develop problems.
Look in the task manager and see if you have multiple versions of word
running

xl2000 purportedly had some problems using the with construct in an
automation environment. Perhaps the error has reappeared in xl2003.

Looks like you have created a reference to the Word Object model (make sure
it is the correct version), so constant definitions shouldn't be a problem.
 
G

Guest

It worked perfectly under Excel XP (2002), but it hasn't worked yet under
Excel 2003. Yes, there are all kinds of WINWORD.EXEs running in the task
manager -- one for every time I have run this and it fails (which is every
time). It runs the code perfectly until it gets to the <.Text=...> line, and
then it crashes Excel!
 
T

Tom Ogilvy

I don't have xl2003 installed here, but running in xl2002 I got an error in

appWordSelection.Find.Execute Replace:=wdReplaceAll

for the obvious reason. It did put an instance or word running in the task
manager. It ran fine after that, but perhaps that might not be the case in
xl2003. I would fix the code, then close excel. Make sure the task manager
has no instances of word running, then try again.
 
G

Guest

Thanks, Tom -- the error you got comes from my mistake: there should be a
period after "appWord" in that line of code -- I went through and added an
appWord object to specifiy the Selection object, since I was calling it from
outside of Word. I just missed the period. That's probably why you got the
error...
 
T

Tom Ogilvy

for the obvious reason.

--
Regards,
Tom Ogilvy

Joel Berry said:
Thanks, Tom -- the error you got comes from my mistake: there should be a
period after "appWord" in that line of code -- I went through and added an
appWord object to specifiy the Selection object, since I was calling it from
outside of Word. I just missed the period. That's probably why you got the
error...
 
H

Hein Heinrichsen

Hello
I think I have the same problem but I didnt really understand the
solution :-((. Can you please give me a hint what to change in the code
to prevent Excel from crashing?
Thank you very much!
Hein


Sub test()
Dim wrdApp As Word.Application
Dim PfadMod2 As String
Set wrdApp = New Word.Application

PfadMod2 = "H:\temp9\GesprächAnfang
März\PSS-Burghaun-Auestr-22-EifertLo"

wrdApp.ChangeFileOpenDirectory _
PfadMod2 '

wrdApp.Documents.Open Filename:="LV.dot",
ConfirmConversions:=False, _
ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _
PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
WritePasswordTemplate:="", Format:=wdOpenFormatAuto
wrdApp.Visible = True
wrdApp.ActiveWindow.ActivePane.View.SeekView =
wdSeekCurrentPageHeader
wrdApp.ActiveWindow.ActivePane.View.NextHeaderFooter
wrdApp.ActiveWindow.ActivePane.View.NextHeaderFooter
wrdApp.Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="1"

wrdApp.Selection.Find.ClearFormatting
With wrdApp.Selection.Find
.Text = "#R#"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
wrdApp.Selection.Find.Execute

End Sub
 

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