Preventing RPC Server Unavailable Error

I

Igor

I have a windows forms program that allows users to open up Word documents
with pre-populated information. I am using interop to code against Word and
opening a document like this: Word._Docuemnt doc1 =
wordApp.Documents.Add(.....)

Now, lets say the user closes Word and then goes back to the application and
tries to open up an instance of Word, they will receive "Invalid Command" and
"RPC Server Unavailable" COM exceptions. Is there any way to prevent this?

Thanks,
Igor
 
C

Cindy M.

Hi Igor,
I have a windows forms program that allows users to open up Word documents
with pre-populated information. I am using interop to code against Word and
opening a document like this: Word._Docuemnt doc1 =
wordApp.Documents.Add(.....)

Now, lets say the user closes Word and then goes back to the application and
tries to open up an instance of Word, they will receive "Invalid Command" and
"RPC Server Unavailable" COM exceptions. Is there any way to prevent this?

There are other reasons for this error, but the one I encounter most commonly is
due to COM resources not being released correctly by your interop code. This
means a "stub" of Winword.exe is still "alive" under Windows, so when your code
tries to "start over" it conflicts with the existing application, perhaps still
linked to a "live" variable in your code.

You can check whether this suggestion is on the right track by looking in the
Windows Task Manager. Is the instance of Winword.exe (linked to wordApp) still
listed there? And if you end that task, will your code then run?

If yes, then you need to make your code COM-compliant. You need to explicitly
set all Word-related variables that may still be in scope (class level) to null
(Nothing in VB) - in the reverse order they were instantiated. Then force their
garbage collection and finalization of the collection - twice. This should reset
the COM pointers to zero, thus releasing the resources.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 

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