Solved

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

Posted on 2014-03-04
11
2,848 Views
Last Modified: 2014-03-23
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?
0
Comment
Question by:CipherIS
  • 4
  • 4
  • 2
11 Comments
 
LVL 40
ID: 39903708
new Microsoft.Office.Interop.Word.DocumentClass()

Do not create a DocumentClass object, create a Document object

new Microsoft.Office.Interop.Word.Document()
0
 
LVL 1

Author Comment

by:CipherIS
ID: 39903912
So I should have?

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

Open in new window

0
 
LVL 1

Author Comment

by:CipherIS
ID: 39903965
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?)
0
 
LVL 40
ID: 39904249
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.
0
 
LVL 1

Author Comment

by:CipherIS
ID: 39906595
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))"
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 39913715
What happens if the Word application is visible?  

Are you getting any errors there?
 

Any dialogs showing?
0
 
LVL 1

Author Comment

by:CipherIS
ID: 39920716
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

0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 39922004
Here are some tweaks in a proposed WordService:

using System;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Word;

namespace OfficeApplications
{
    public class WordService : IDisposable
    {

        private readonly Application _wApp;
        private _Document _masterDoc;

        private static WordService _instance;

        public bool IsAppVisible
        {
            get { return _wApp.Visible; }
            set { _wApp.Visible = value; }
        }

        public static WordService GetInstance()
        {
            return _instance ?? (_instance = new WordService());
        }

        private WordService()
        {
            _wApp = new Application
            {
                Visible = false,
                DisplayAlerts = WdAlertLevel.wdAlertsNone,
            };

            _wApp.Options.SuggestSpellingCorrections = false;
            _wApp.Options.CheckGrammarAsYouType = false;
            _wApp.Options.CheckSpellingAsYouType = false;
            _wApp.Options.CheckGrammarWithSpelling = false;
        }

        public void LoadDocument(string fileName)
        {
            _masterDoc = _wApp.Documents.Open(fileName);
            _masterDoc.Activate();
        }

        public void Dispose()
        {
            if (_masterDoc != null)
            {
                _masterDoc.Close(false);
                Marshal.ReleaseComObject(_masterDoc);
            }

            if (_wApp == null)
                return;

            _wApp.Quit();
            Marshal.ReleaseComObject(_wApp);
        }
    }
}

Open in new window

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39922009
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

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39922017
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.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

20 Experts available now in Live!

Get 1:1 Help Now