• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 854
  • Last Modified:

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")

or

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.
0
AIBMass
Asked:
AIBMass
  • 3
  • 2
2 Solutions
 
CodeCruiserCommented:
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?
0
 
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?
0
 
CodeCruiserCommented:
Marshal.ReleaseComObject method is one way.
0
 
AIBMassAuthor 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.

http://managedupnp.codeplex.com/discussions/276221

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.
0
 
AIBMassAuthor Commented:
Marshal.ReleaseComObject is certainly a valuable tool, but not really relevant here. The real key is that Visual Studio is the culprit.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now