Microsoft Automation of MS-Word 2007 using VB.NET 2008 programming COMException
Posted on 2013-01-14
Hello EE Experts,
I am very suprised to see so few answered questions on the topic of Microsoft Automation of MS-Word using Visual Basic .NET (2008 in this case) programming there are on EE. The word "Automation" has a very special meaning in this context. So much benefit can be had in businesses of all sizes for the production of documents that are specific to the recipient -- not just junk mail. That is what my VB.NET 2008 program does. Produce documents for attorneys that have data inserted in them by the MS-Word Mail Merge process, but I am having a problem. After the document is produced and saved, I display the file name in a DataGrid that the user can click on to select and click on a button to "open" the MS-Word document. This opens the document in MS-Word using MS-Word as what Microsoft calls an "Automation Server".
The problem that I am having is this. Early in the start up of the program I declare an object reference variable to point to an instance of MS-Word, and instantiate an instance of MS-Word using MS-Automation techniques. No big deal. Here is the code that works:
Public go_wrdApp As Microsoft.Office.Interop.Word.Application
go_wrdApp = CreateObject("Word.Application")
So now go_wrdApp is an object reference variable that points to an instance of the MS-Word.program (an Automation Server)
Using Microsoft "Automation" technique -- Each of the Microsoft Office program components can be a Microsoft "Automation Server" -- I can run the mail merge process and save each MS-Word document generated into a file to be opened later as described above. The problem occurs when the user closes the MS-Word application frame after looking at a documet by clicking on the red 'X' in the upper right corner of the application frame. This closes the whole instance of MS-Word -- not just the document that was being viewed in the application program.
So now, at a later time, when the same user attempts to look at another MS-Word document using the technique described above with the DataGrid, instead of opening the document in MS-Word the VB.NET 2008 program throws a COMException with the message "RPC server is unavailable" because the object reference variable go_wrdApp was left pointing at a MS-Word program object (Automation Server) that no longer exists because the user clicked on the little red 'X' in the upper right corner of the application frame instead of just closing only the document.
Is there a way that I can test "go_wrdApp" object reference variable to be sure that it is pointing at an instance of MS-Word program object that is really there?
"If Not IsNothing(go_wrdApp) Then" doesn't work.
Oh! I believe the reference to RPC in the error message means "Remote Procedure Call".