Trouble with Word Automation - Context has been disconnect
Posted on 2012-09-13
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.