Solved

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

Posted on 2014-03-04
11
3,067 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

617 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