Link to home
Start Free TrialLog in
Avatar of CipherIS
CipherISFlag for United States of America

asked on

C# MS Word Interop on Document.Activate Error RPC_E_Disconnect 0x80010108 Error

I am receiving an RPC_E_Disconnect 0x80010108 Error when my code gets to masterDoc.Activate();

The below code works

private Microsoft.Office.Interop.Word._Application wApp;
private Microsoft.Office.Interop.Word._Document masterDoc;

private void Initialize() 
{
    wApp = new Microsoft.Office.Interop.Word.ApplicationClass();
wApp.Visible = false;
wApp.Options.SuggestSpellingCorrections = false;
wApp.Options.CheckGrammarAsYouType = false;
wApp.Options.CheckSpellingAsYouType = false;
wApp.Options.CheckGrammarWithSpelling = false;

    masterDoc = new Microsoft.Office.Interop.Word.Document();
missing = System.Reflection.Missing.Value;
    oPageBreak = Microsoft.Office.Interop.Word.WdBreakType.wdSectionBreakNextPage;
masterDoc =  wApp.Documents.Add(ref missing, ref missing, ref missing, ref oFalse);
masterDoc.Application.Visible = false;
masterDoc.Application.Options.SuggestSpellingCorrections = false;
masterDoc.Application.Options.CheckGrammarAsYouType = false;
masterDoc.Application.Options.CheckSpellingAsYouType = false;
masterDoc.Application.Options.CheckGrammarWithSpelling = false;
}

private int loadDocument(object wordFileName)
{
    Microsoft.Office.Interop.Word._Document currentDoc = new 
        Microsoft.Office.Interop.Word.DocumentClass();
currentDoc.Application.Visible=false;
    currentDoc.Application.Options.SuggestSpellingCorrections = false;
currentDoc.Application.Options.CheckGrammarAsYouType = false;
currentDoc.Application.Options.CheckSpellingAsYouType = false;
currentDoc.Application.Options.CheckGrammarWithSpelling = false;
    currentDoc.Application.DisplayAlerts = 
        Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone;

   masterDoc.Activate();  //ERROR OCCURS HERE
}

Open in new window


The below code results in an error in the method above.

 masterDoc.Activate();  //ERROR OCCURS HERE

Open in new window


I am receiving the below error:

The object invoked has disconnected from its clients. (Exception from HRESULT: 0x80010108 (RPC_E_DISCONNECTED))"

Any idea how to resolve?
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

new Microsoft.Office.Interop.Word.DocumentClass()

Do not create a DocumentClass object, create a Document object

new Microsoft.Office.Interop.Word.Document()
Avatar of CipherIS

ASKER

So I should have?

private Microsoft.Office.Interop.Word._Application wApp;
private Microsoft.Office.Interop.Word._Document masterDoc;

Open in new window

That didn't work.  

private Microsoft.Office.Interop.Word.Document wApp;
private Microsoft.Office.Interop.Word._Document masterDoc;

wApp = new Microsoft.Office.Interop.Word.Document

Open in new window


Error now says:

'Microsoft.Office.Interop.Word.Document' does not contain a definition for 'Quit' and no extension method 'Quit' accepting a first argument of type 'Microsoft.Office.Interop.Word.Document' could be found (are you missing a using directive or an assembly reference?)
Be careful with the details. Programming is all about détails. I told you to use Document instead of DocumentClass. You replaced ApplicationClass insted of DocumentClass.

You should have:

Microsoft.Office.Interop.Word.Application wApp;
 Microsoft.Office.Interop.Word.Document masterDoc;

wApp = new Microsoft.Office.Interop.Word.Application();
masterDoc = new Microsoft.Office.Interop.Word.Document(); // This one you had OK from the start

Microsoft.Office.Interop.Word.Document currentDoc = new 
        Microsoft.Office.Interop.Word.Document;

Open in new window


That way, wApp.Quit() will work. It did not work in your last post, because you defined wApp as a Document instead of an Application. You do not Quit a Document, you Quit an Application.
I made the suggested changes but I'm still receiving below error:

The object invoked has disconnected from its clients. (Exception from HRESULT: 0x80010108 (RPC_E_DISCONNECTED))"
Avatar of Bob Learned
What happens if the Word application is visible?  

Are you getting any errors there?
 

Any dialogs showing?
When I set visible = true it opens word.  No issues.  I can see the Word documents open and populate.  It only breaks on

masterDoc.Activate()

    masterDoc.Activate();

    masterDoc.Select();
    masterDoc.Application.Selection.NoProofing = 1;  
    masterDoc.Application.Selection.MoveEnd(ref missing, ref missing);
    masterDoc.Application.Selection.MoveRight(ref missing, ref missing, ref missing);  
    masterDoc.Application.Options.SuggestSpellingCorrections = false;
    masterDoc.Application.Options.CheckGrammarAsYouType = false;
    masterDoc.Application.Options.CheckSpellingAsYouType = false;
    masterDoc.Application.Options.CheckGrammarWithSpelling = false;
    masterDoc.Application.DisplayAlerts =   
        Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone;

    wApp.Selection.Paste();

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sample usage:

            var fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
                ".NET ASP.NET Technical Interview Questions.docx");

            var wordService = WordService.GetInstance();
            wordService.LoadDocument(fileName);
            wordService.IsAppVisible = true;

Open in new window

Notes:

1) You need to call Marshal.ReleaseComObject to release memory for COM objects, since .NET doesn't use reference counting.  That method decrements the object reference count.

2) .NET 4.0 and higher uses optional parameters, so you don't need all those "missing" values.

3) I believe that you should be using Document.Open, and not Document.Add to open the Word document.

4) I added IDisposable, so you could use this call in a using block.  Upon completion of the code, the WordService.Dispose will automatically be called.

using (var wordService = WordService.GetInstance())
{
}

Open in new window


5) WordService is a singleton class, with a private constructor, and a static GetInstance.  You can't use the "new" keyword with singleton classes, as the constructor is private.