Oracle データベースから何千もの単語文書を取得し、それらを pdf に変換してデータベースに送り返す必要があるアプリケーションを作成しています。私はすでにすべてのサポートメカニズム (データベースの相互作用、マルチタスキング、およびデータベースと構成へのプラグ可能なアプローチ) を稼働させています。サーバー側でオフィス オートメーションを使用することについての警告にもかかわらず、私の最初のアプローチはそれを使用することでした (実際には、私の顧客はそれを使用するように求めました)。しかし、私は c# (.Net 4.0) と Word 2007 の間の相互作用に頭がおかしくなっています。私はすでに SaveAs と ExportAsFixedFormat を試しました。どちらも問題なく動作しましたが、単語を閉じようとすると...エラーが表示されました(単語に問題が見つかり、閉じられることを示すポップアップウィンドウ)。次に、アプリケーションを終了する前にこれを含めようとしました:
wordApplication.NormalTemplate.Saved = true;
しかし、それでもエラーが発生します。エラーなしで 100 を超えるドキュメントを変換できません。オフィスオートメーションを使用せずにこの変換を達成する方法を知っていますか? あるいは、オフィス オートメーションを使用してこの変換をエラーなく行う方法を知っていますか? どんな助けでも大歓迎です。
編集:オタク、これは私が使用しているコードの例です(警告!コードを先にテストしてください)
if (wordApplication == null)
{
try
{
wordApplication = (ApplicationClass)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application") ??
new ApplicationClass();
}
catch (COMException)
{
Type type = Type.GetTypeFromProgID("Word.Application");
wordApplication = (ApplicationClass)System.Activator.CreateInstance(type);
}
}
wordApplication.DisplayAlerts = WdAlertLevel.wdAlertsNone;
wordApplication.DisplayRecentFiles = false;
wordApplication.Visible = false;
wordApplication.ScreenUpdating = false;
Document wordDocument = null;
object paramSourceDocPath = Path.Combine(TempFolder, sourceFilename);
var paramExportFilePath = Path.Combine(TempFolder, sourceFilename + ".pdf");
var paramMissing = Type.Missing;
const WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatPDF;
const bool paramOpenAfterExport = false;
const WdExportOptimizeFor paramExportOptimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint;
const WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;
const int paramStartPage = 0;
const int paramEndPage = 0;
const WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;
const bool paramIncludeDocProps = true;
const bool paramKeepIrm = true;
const WdExportCreateBookmarks paramCreateBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks;
const bool paramDocStructureTags = true;
const bool paramBitmapMissingFonts = true;
const bool paramUseIso190051 = false;
try
{
// Open the source document.
wordDocument = wordApplication.Documents.Open(
ref paramSourceDocPath, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing);
// Export it in the specified format.
if (wordDocument != null)
{
//DocumentSaveAs(wordDocument);
Logger.Write("Open document" + sourceFilename, "info");
wordDocument.ExportAsFixedFormat(paramExportFilePath,
paramExportFormat, paramOpenAfterExport,
paramExportOptimizeFor, paramExportRange, paramStartPage,
paramEndPage, paramExportItem, paramIncludeDocProps,
paramKeepIrm, paramCreateBookmarks, paramDocStructureTags,
paramBitmapMissingFonts, paramUseIso190051,
ref paramMissing);
}
}
catch (Exception ex)
{
Logger.Write(ex.Message);
throw;
}
catch
{
Logger.Write("Empty catch.");
throw;
}
finally
{
try
{
object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
// Close and release the Document object.
if (wordDocument != null)
{
wordDocument.Close(ref saveChanges, ref paramMissing,
ref paramMissing);
Thread.Sleep(2000);
wordDocument = null;
}
// Quit Word and release the ApplicationClass object.
foreach (Document document in wordApplication.Documents)
{
document.Close(saveChanges, paramMissing, paramMissing);
}
wordApplication.NormalTemplate.Saved = true;
wordApplication.Quit(ref saveChanges, ref paramMissing,
ref paramMissing);
//Thread.Sleep(1000);
wordApplication = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
Logger.Write("Deleting word file " + sourceFilename, "info");
File.Delete(paramSourceDocPath.ToString());
Logger.Write("File deleted " + sourceFilename, "info");
Logger.Write("Reading pdf data " + paramExportFilePath, "info");
ret = File.ReadAllBytes(paramExportFilePath);
Logger.Write("Data read " + sourceFilename + ".pdf", "info");
File.Delete(paramExportFilePath);
Logger.Write("Pdf file deleted " + paramExportFilePath, "info");
}
catch (Exception e)
{
Logger.Write(e,"info");
throw;
}