COMException: The object invoked has disconnected from its clients

3

I have a program written in C# that creates a Word doc by collating a bunch of text (extracted from objects). This application has worked fine for the past 4 years on many different machines, but right now it is breaking for one new client with the following error:

System.Runtime.InteropServices.COMException (0x80010108): The object invoked has disconnected from its clients. (Exception from HRESULT: 0x80010108 (RPC_E_DISCONNECTED)) at Microsoft.Office.Interop.Word.DocumentClass.get_Content() at MyCompany.MyApp.Main.btnPrint_Click(Object sender, EventArgs e) at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Here's the code snippet (it won't compile as-is because this is just an excerpt, but it should make sense):

// This will be the collated doc
object missing = System.Type.Missing;
Document combinedDoc = null;

// Temp doc holders
string tempWordFilePath;
object tempWordFilePathObj;

// Loop thru records and add their content to Word doc
foreach (RecordWrapper rec in records)
{
    // Decode base64 attachment to byte-array
    byte[] b = decodeToBase64(rec.body);

    if (combinedDoc == null) combinedDoc = app.Documents.Add(ref missing, ref missing, ref missing, ref missing);

    tempWordFilePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\" + (string)o.Item("Name").Value;
    tempWordFilePathObj = tempWordFilePath;

    if (File.Exists(tempWordFilePath))
    {
        File.Delete(tempWordFilePath);
    }

    // Write bytes into a temp Word doc
    FileStream fs = new FileStream(tempWordFilePath, FileMode.CreateNew);
    fs.Write(b, 0, b.Length);
    fs.Close();

    // Load the temp file as a Document
    Document doc = app.Documents.Open(ref tempWordFilePathObj, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

    // Insert page break
    object collStart = combinedDoc.Content.Start;
    object collEnd = combinedDoc.Content.End;
    Range rng2 = combinedDoc.Range(ref collStart, ref collEnd);
    object collapseEnd = Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd;
    rng2.Collapse(ref collapseEnd);

    // Paste range into resulting doc
    rng2.InsertFile(tempWordFilePath, ref missing, ref missing, ref missing, ref missing);

    object pageBrk = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;
    rng2.InsertBreak(ref pageBrk);

    doc.Close(ref missing, ref missing, ref missing);
    File.Delete(tempWordFilePath);
}

I have read on an MSDN forum that this may be due to a missing library called SHDocVw.dll. I have repackaged my application with said library included but it's the same result. Others have said that it may be a Service Pack issue, but there haven't been any recommended solutions. Another person has recommended to ignore "80010108" errors but the idea was quickly dismissed by the OP. I have also read on here that this could be due to incorrect instantiation/referencing of certain Interop classes, but I don't see that happening in my code (or am I not seeing it?)

ms-word
interop
rpc
comexception
shdocvw
asked on Stack Overflow Aug 15, 2016 by Mossi • edited May 23, 2017 by Community

1 Answer

0

I had the same issue on a Windows 2008 server with Microsoft Office 2013.

Try to repair your Microsoft Office installation.

On control panel choose the repair option and reset your PC.

It worked for me!

answered on Stack Overflow Feb 17, 2020 by Luis Márquez

User contributions licensed under CC BY-SA 3.0