Solved

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

Posted on 2014-03-04
11
3,003 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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!

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…

734 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