Error controlling Word from Excel

J

Jason

Hi,

I wrote a simple (I thought) macro to populate a Word document from
Excel (both are Office 2003). I seem to be doing something wrong at
the end of the macro when I kill off the Word object, because the
macro appears to work fine the first time I invoke it and then fails
the next time with the error message "Run-time error '462': The remote
server machine does not exist or is unavailable." I then have to go
into the Task Manager and manually kill the Winword process. When I
do this, I can then run the macro again--once--before getting the
error again the second time I run it. It's frustrating, and I'd
really appreciate it if anyone can lend their wisdom and help me make
it fully functional.

Here's the applicable code (repetitive code in the middle has been
removed for clarity):

Sub Populate_Report()

Dim WordApp As New Word.Application

Set WordApp = CreateObject("Word.Application")
WordApp.Documents.Open ("C:\Report.doc")

Word.ActiveDocument.Bookmarks("Name").Select 'NOTE: This is the
indicated line when the macro breaks
Word.Selection.TypeText NameValue
....

Word.ActiveDocument.SaveAs Filename:="C:\" & ReportName & ".doc"

WordApp.Quit
Set WordApp = Nothing

My thanks to anyone who can help me.

Best wishes...

....Jay
 
J

Jason

Hi,

Following a link I found elsewhere, I actually managed to solve my own
problem. I'm posting the solution here in the hopes that it might help
someone else who encounters the same problem.

Using this Microsoft Knowledge Base post
(http://support.microsoft.com/kb/189618/EN-US/), I figured out that I was not
referring to my Word object properly, as I had initialized it as WordApp, but
was attempting to control it via the Word object alone.

Old, incorrect version:
Word.ActiveDocument.Bookmarks("Name").Select

New, correct version:
WordApp.ActiveDocument.Bookmarks("Name").Select

It works like a treat now. I still don't understand why it semi-worked
before, but I guess that's not as important.

Thanks to anyone who looked at this with an eye toward helping out.

Best wishes...

....Jay
 

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