CipherIS
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
The below code results in an error in the method above.
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?
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
}
The below code results in an error in the method above.
masterDoc.Activate(); //ERROR OCCURS HERE
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?
ASKER
So I should have?
private Microsoft.Office.Interop.Word._Application wApp;
private Microsoft.Office.Interop.Word._Document masterDoc;
ASKER
That didn't work.
Error now says:
'Microsoft.Office.Interop. Word.Docum ent' does not contain a definition for 'Quit' and no extension method 'Quit' accepting a first argument of type 'Microsoft.Office.Interop. Word.Docum ent' could be found (are you missing a using directive or an assembly reference?)
private Microsoft.Office.Interop.Word.Document wApp;
private Microsoft.Office.Interop.Word._Document masterDoc;
wApp = new Microsoft.Office.Interop.Word.Document
Error now says:
'Microsoft.Office.Interop.
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:
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.
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;
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.
ASKER
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))"
The object invoked has disconnected from its clients. (Exception from HRESULT: 0x80010108 (RPC_E_DISCONNECTED))"
What happens if the Word application is visible?
Are you getting any errors there?
Any dialogs showing?
ASKER
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.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();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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;
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.
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.
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())
{
}
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.
Do not create a DocumentClass object, create a Document object
new Microsoft.Office.Interop.W