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

问题描述 投票:1回答:1

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

System.Runtime.InteropServices.COMException(0x80010108):对象被调用已与其客户端断开连接。 (来自HRESULT的异常:0x80010108(RPC_E_DISCONNECTED))在Microsoft.Office.Interop.Word.DocumentClass.get_Content()在MyCompany.MyApp.Main.btnPrint_Click(对象发送者,EventArgs 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(Message&m)在System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd,Int32msg,IntPtr wparam,IntPtr lparam)

这是代码段(由于这只是摘录,所以它不能按原样编译,但是应该有道理:):

// 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);
}

我在MSDN论坛上读到,这可能是由于缺少名为SHDocVw.dll的库造成的。我已经用所说的库重新包装了我的应用程序,但是结果是一样的。其他人则说这可能是Service Pack的问题,但是没有任何推荐的解决方案。另一个人推荐了ignore "80010108" errors,但是这个想法很快被OP驳回了。我还阅读了on here,这可能是由于某些Interop类的实例化/引用不正确造成的,但是我没有在代码中看到这种情况(或者我没有看到吗?)

ms-word interop rpc comexception shdocvw
1个回答
0
投票

我在装有Microsoft Office 2013的Windows 2008服务器上遇到相同的问题。

尝试修复您的Microsoft Office安装。

在控制面板上选择修复选项并重置您的PC。

对我有用!

© www.soinside.com 2019 - 2024. All rights reserved.