【发布时间】:2016-12-22 05:07:26
【问题描述】:
我有一个用 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 事件) 在 System.Windows.Forms.Control.WmMouseUp(消息和 m,鼠标按钮 按钮,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(消息& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息& m) 在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
这是代码 sn-p(它不会按原样编译,因为这只是摘录,但应该有意义):
// 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 的库。我已经使用包含的库重新打包了我的应用程序,但结果相同。其他人说这可能是服务包问题,但没有任何推荐的解决方案。另一个人向ignore "80010108" errors 推荐了这个想法,但这个想法很快就被 OP 驳回了。我还阅读了on here,这可能是由于某些互操作类的实例化/引用不正确,但我没有看到我的代码中发生了这种情况(或者我没有看到它?)
【问题讨论】:
-
客户是否提供自己的 MS Word?他们是否有您期望的版本,否则它是否运作良好?是否获得许可?
-
@asynchronos 是的,他们提供自己的 MS Word,这是预期的版本(Office Pro 2013)。它可以正常工作并且已获得许可。
-
一个谷歌点击提到了一个导致这个错误的自定义加载项(虽然方法不同)。 www-01.ibm.com/support/docview.wss?uid=swg21609608
-
@Mossi 你找到解决方案了吗?在获取起始值对象 collStart = combinedDoc.Content.Start; 时,我也可能遇到相同的错误
标签: ms-word interop rpc comexception shdocvw