Ms word COMException:调用的对象已与其客户端断开连接

Ms word COMException:调用的对象已与其客户端断开连接,ms-word,interop,rpc,comexception,shdocvw,Ms Word,Interop,Rpc,Comexception,Shdocvw,我有一个用C#编写的程序,它通过整理一堆文本(从对象中提取)来创建Word文档。在过去的4年中,该应用程序在许多不同的机器上运行良好,但现在一个新客户端出现以下错误: System.Runtime.InteropServices.COMException(0x80010108):对象 已从其客户端断开连接。(HRESULT的例外情况: 0x80010108(RPC_E_已断开))位于 Microsoft.Office.Interop.Word.DocumentClass.get_Content(

我有一个用C#编写的程序,它通过整理一堆文本(从对象中提取)来创建Word文档。在过去的4年中,该应用程序在许多不同的机器上运行良好,但现在一个新客户端出现以下错误:

System.Runtime.InteropServices.COMException(0x80010108):对象 已从其客户端断开连接。(HRESULT的例外情况: 0x80010108(RPC_E_已断开))位于 Microsoft.Office.Interop.Word.DocumentClass.get_Content()位于 MyCompany.MyApp.Main.btnPrint\u单击(对象发送方,事件参数e) 在System.Windows.Forms.Control.OnClick(EventArgs e)中 System.Windows.Forms.Button.OnClick(EventArgs e)位于 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs-mevent)位于 System.Windows.Forms.Control.WmMouseUp(Message&m,MouseButtons 按钮,Int32单击)在 System.Windows.Forms.Control.WndProc(Message&m)位于 System.Windows.Forms.ButtonBase.WndProc(Message&m)位于 System.Windows.Forms.Button.WndProc(Message&m)位于 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m) 在System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息& m) 在System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd,Int32 msg、IntPtr wparam、IntPtr lparam)

下面是代码片段(它不会按原样编译,因为这只是一个摘录,但应该有意义):


我在一个MSDN论坛上读到,这可能是因为缺少一个名为SHDocVw.dll的库。我已经重新包装了我的应用程序,包括上述库,但结果是一样的。其他人说这可能是一个Service Pack问题,但还没有任何推荐的解决方案。另一个人建议这样做,但该想法很快被OP驳回。我还了解到,这可能是由于某些互操作类的实例化/引用不正确造成的,但我的代码中没有出现这种情况(或者我没有看到?

我在Windows 2008服务器和Microsoft Office 2013上也遇到了同样的问题

请尝试修复您的Microsoft Office安装

在“控制面板”上选择“修复”选项并重置电脑


这对我有用

客户是否提供自己的MS Word?他们有你期望的版本吗?在其他方面是否工作良好?是否获得许可?@asynchronos是的,他们提供自己的MS Word,这是预期版本(Office Pro 2013)。谷歌的一次点击提到了一个自定义插件导致了这个错误(尽管使用了不同的方法)@摩西,你找到解决办法了吗?Ia malso在获取开始值对象collStart=combinedDoc.Content.start时遇到相同错误;
// 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);
}