ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Error controlling Word from Excel (https://www.excelbanter.com/excel-programming/419537-error-controlling-word-excel.html)

Jason[_42_]

Error controlling Word from Excel
 
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

Jason

Error controlling Word from Excel
 
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

"Jason" wrote:

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



All times are GMT +1. The time now is 05:50 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com