Trouble with Word Automation - Context has been disconnect

I have a VB.Net Winforms app that has been doing Word automation successfully for years.
Now, suddenly, I get occasional errors saying:

DisconnectedContext was detected.....ensure that all contexts/apartments stay alive until the application is completely done with the RuntimeCallableWrappers that represent COM components that live inside them.

The automation of Word involves extensive cutting, pasting, copying, appending and replacing to form a single new document from about 15 other documents.

I only get the error during extreme cases, that is, when there is much more data than usual to integrate into the document.

I make a lot of use of VB subroutines into which I pass Word.Document objects byref.

Does anyone have an idea what, if any, threshold I might be reaching? Or time limit? Or a way to tell even what object has lost its context?

Most of all my VB variables are class level. For example:

Private wordApp as Word.Application

which is created with

Me.wordApp = CreateObject("Word.Application")


Private rnge as Word.Range

My app is .NET 3.5 and my Word DLL comes from Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office14\Microsoft.Office.Interop.Word.dll

And leads greatly appreciated.
Who is Participating?
AIBMassConnect With a Mentor Author Commented:
Well, it took a great deal of research, but it now appears that this is a Visual Studio issue.

The article mentioned below describes how to disable/hide the troublesome errors, which are not actually errors at all.

Following the instructions to no longer break when a DisconnectedContext error is found allows everything to work.

The question of why these errors began to appear at all remains a mystery. And apologies for not mentioning that the errors were found in Visual Studio.
This has to do with not releasing COM components properly. When you say you read from multiple documents, do you dispose objects linking to those documents properly?
AIBMassAuthor Commented:
My Word application is opened once and immediately multiple documents are read.
This results in up to 17 documents. Then data is read from the database and this causes replace, append and format actions to occur in the documents.

At the end, all of the documents except one are closed.

The range objects are almost all at the class level.

When the process fails, it fails in the middle somewhere of the document manipulations. I need all of the documents from the beginning through to the end.

What is the right way to dispose of the COM objects?
CodeCruiserConnect With a Mentor Commented:
Marshal.ReleaseComObject method is one way.
AIBMassAuthor Commented:
Marshal.ReleaseComObject is certainly a valuable tool, but not really relevant here. The real key is that Visual Studio is the culprit.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.