Trouble with Word Automation - Context has been disconnect

Posted on 2012-09-13
Last Modified: 2012-09-19
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.
Question by:AIBMass
    LVL 83

    Expert Comment

    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?

    Author Comment

    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?
    LVL 83

    Assisted Solution

    Marshal.ReleaseComObject method is one way.

    Accepted Solution

    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.

    Author Closing Comment

    Marshal.ReleaseComObject is certainly a valuable tool, but not really relevant here. The real key is that Visual Studio is the culprit.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
    A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
    This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
    This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now