オブジェクトから抽出された一連のテキストを照合して Word ドキュメントを作成する C# で記述されたプログラムがあります。このアプリケーションは過去 4 年間、さまざまなマシンで正常に動作していましたが、現在、1 つの新しいクライアントで次のエラーが発生して機能しなくなりました。
System.Runtime.InteropServices.COMException (0x80010108): 呼び出されたオブジェクトがクライアントから切断されました。(HRESULT からの例外: 0x80010108 (RPC_E_DISCONNECTED)) 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(メッセージ&
コード スニペットは次のとおりです (これは単なる抜粋であるため、そのままではコンパイルされませんが、意味があるはずです)。
// 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 の問題である可能性があると言う人もいますが、推奨される解決策はありません。別の人が「80010108」エラーを無視することを推奨しましたが、そのアイデアは OP によってすぐに却下されました。これは、特定の相互運用クラスのインスタンス化/参照が正しくないことが原因である可能性があることもここで読みましたが、私のコードでそれが起こっていることはわかりません (または、表示されていませんか?)