我在用Microsoft Office Interop Word
使用 C# 生成文档。为了使文档生成正常工作,应该有一个条目"Microsoft Office Word 97 - 2003 Document"
在下面Dcom Config Settings
如下所示:
The Local Path
在下面General Tab
首次安装 Microsoft Office 时具有正确的路径。如果我随后将计算机加入域,然后使用域用户重新启动系统,Local Path
变为空白,应用程序不会生成文档并给出错误。
即使我先将计算机加入域,然后使用域用户登录,然后安装 Microsoft Office,Local Path
首先显示正确,然后重新启动后,它又变成空白。同时,如果我使用Local User
, the Path
仍然在那里。
是什么造成了价值Local Path
变成空白?
所有设置都在虚拟机上进行,并且“自动化”一词在域帐户上工作,就像我看到它在加入域的物理机上工作一样。
UPDATE: What my application is doing:
我的应用程序中有 4-5 个组件。
第一个是 VSTO Word AddIn,它与 Microsoft Word 集成,我们在其中创建包含一些也保存在数据库中的表达式的新文档。表达式也有条件,它们也可以嵌套。表达式包含来自保存在数据库中的 XSD 文件的架构元素。创建此类文档后,其 WordML 将保存在数据库中。这一切都是在 VSTO AddIn 中完成的。
第二个是一个 Web 服务,它从另一个组件接收输入 xml,该组件与上面的 XSD 进行确认,其中架构元素已嵌入到通过 VSTO addIn 创建的文档中的表达式中。该 Web 服务检查验证和其他几个任务。然后,它从数据库获取相应 Word 文档的 WordML,并将其传递给 Word Interop,Word Interop 使用其 API 递归地迭代它,以将架构元素替换为输入 xml 中的实际值。然后将 WordML 作为 Word 文档保存到文件中。
这还会在保存文档之前将模板附加到文档。它还使用 Word Interop 的 SaveAs 功能将文件另存为 PDF。
更新:
我再次浏览了完整的应用程序,并了解到我们正在通过解析 Office Open XML 来完成所有操作(例如,将输入提供给 Word 文档),但我们使用 Word Automation 所做的唯一事情如下:
- 使用 Word Interop 将生成的 WordML 保存为 Word 格式文件之一。
- 将生成的 WordML 导出到 PDF 文件。
- 将多个 WordML 合并到一个 Word 文档文件中。
- 为其获取 XML。
所有这四个代码如下所示,仅包含相关部分的代码:
Microsoft.Office.Interop.Word.Document wordDocument = null;
object templateName = "templateFile.dotm";
wordDocument = this.WordApplication.Documents.Add(ref missing, ref missing, ref missing, ref missing);
wordDocument.Range(ref missing, ref missing).Text = "";
wordDocument.set_AttachedTemplate(ref templateName);
wordDocument = this.WordApplication.Documents.Open(
ref objSourceFilePath, ref oFalse, ref oTrue,
ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing,
ref oMissing);
wordDocument.ExportAsFixedFormat(
strTargetPath,
targetFormat,
paramOpenAfterExport,
paramExportOptimizeFor,
paramExportRange,
paramStartPage,
paramEndPage,
paramExportItem,
paramIncludeDocProps,
paramKeepIRM,
paramCreateBookmarks,
paramDocStructureTags,
paramBitmapMissingFonts,
paramUseISO19005_1,
ref oMissing);
object SaveToFormat = SaveToFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument97;
wordDocument.SaveAs(ref objTargetFilePath, ref SaveToFormat, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
对于合并多个文件:
Microsoft.Office.Interop.Word.Document doc = null;
Microsoft.Office.Interop.Word.Section section = null;
object sectionBreakNextPage = (object)WdBreakType.wdSectionBreakNextPage;
WordApp.Visible = false;
doc = this.WordApplication.Documents.Add(ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing);
if (doc != null)
{
doc.Activate();
int fileCount = sourceFiles.Length;
String fileName = string.Empty;
for (int fileIndex = 0; fileIndex < fileCount; fileIndex++)
{
fileName = sourceFiles[fileIndex];
if (System.IO.File.Exists(fileName))
{
section = doc.Sections.Last;
//delink the current section's header & footer from previous section's header & footer
section.Headers[WdHeaderFooterIndex.wdHeaderFooterFirstPage].LinkToPrevious = false;
section.Footers[WdHeaderFooterIndex.wdHeaderFooterFirstPage].LinkToPrevious = false;
section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].LinkToPrevious = false;
section.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].LinkToPrevious = false;
section.Headers[WdHeaderFooterIndex.wdHeaderFooterEvenPages].LinkToPrevious = false;
section.Footers[WdHeaderFooterIndex.wdHeaderFooterEvenPages].LinkToPrevious = false;
section.Range.InsertFile(fileName, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing);
//if it is last iteration, do'nt insert break
if (fileIndex < fileCount - 1)
{
object rangeStart = (object)(section.Range.End - 1);
doc.Range(ref rangeStart, ref paramMissing).InsertBreak(ref sectionBreakNextPage);
}
}
}
doc.SaveAs(ref targetFile, ref wordFormat, 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);
return true;
}
现在,我收到以下错误:
The message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER))
这一切可以在不使用 Word Automation 的情况下完成吗?