Microsoft Office で非常に奇妙な問題が発生しています。
私は共通のライブラリを持っています。その唯一の目的は、渡されたWordドキュメントのファイルタイプを(完全なファイルパスで...)開き、その開いたWordドキュメントをpdfファイルとして保存することです。
奇妙な問題は、Windows サービスからそのライブラリを使用すると、Word 文書を開こうとするたびに null が返されることです...別名、Word 文書が開かれませんでした。
ただし、WPF または Windows フォーム アプリケーションからライブラリを使用する場合、問題はありません。スレッド (シングル スレッド アパートメント) に問題があることは承知していますが、Windows サービスから問題を解決する方法がわかりません。:( :( :(
助けていただければ幸いです!私が得るエラーは次のとおりです。
例外メッセージ: {"オブジェクト参照がオブジェクトのインスタンスに設定されていません。"} (Word ドキュメントを参照)。内部例外: Null; HResult: -2147467261. データ: エントリが 0 の ListDictionaryInternal。スタック トレース: c:\Project Files...\DocumentConverter.cs:line 209 の DocumentConverter.ToPdf(String currentWorkingFolderPath, String pathToDocumentToConvert) で
これがライブラリ関数です。Visual Studio Tools for Office によって作成される Microsoft Office 参照が必要です。
private string ToPDF(string currentWorkingFolderPath, string pathToDocumentToConvert)
{
string temporaryPdfFolderPath = Path.GetFullPath(currentWorkingFolderPath + "\\pdf\\");
string temporaryPdfFilePath = Path.GetFullPath(temporaryPdfFolderPath + "\\pdffile.pdf");
if (!FileSystem.CreateDirectory(temporaryPdfFolderPath))
{
return null;
}
try
{
Microsoft.Office.Interop.Word.Application wordApplication = new Microsoft.Office.Interop.Word.Application();
object objectMissing = System.Reflection.Missing.Value;
wordApplication.Visible = false;
wordApplication.ScreenUpdating = false;
FileInfo wordFile = new FileInfo(pathToDocumentToConvert);
Object fileName = (Object)wordFile.FullName;
// This is where it breaks when called from windows service. Use the dummy value as a placeholder for optional arguments
Document wordDocument = wordApplication.Documents.Open(ref fileName, ref objectMissing,
true, ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing,
ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing,
ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing);
object outputFileName = (object)temporaryPdfFilePath;
object fileFormat = WdSaveFormat.wdFormatPDF ;
// Save document into PDF Format
wordDocument.SaveAs(ref outputFileName,
ref fileFormat, ref objectMissing, ref objectMissing,
ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing,
ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing,
ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing);
// Close the Word document, but leave the Word application open.
// doc has to be cast to type _Document so that it will find the
// correct Close method.
object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
((_Document)wordDocument).Close(ref saveChanges, ref objectMissing, ref objectMissing);
wordDocument = null;
// word has to be cast to type _Application so that it will find
// the correct Quit method.
((Microsoft.Office.Interop.Word._Application)wordApplication).Quit(ref objectMissing, ref objectMissing, ref objectMissing);
wordApplication = null;
}
catch (Exception ex)
{
//logging code
return null;
}
return temporaryPdfFilePath;
}