Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-03-04
11
Medium Priority
?
3,199 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Screencast - Getting to Know the Pipeline

926 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