【发布时间】:2014-07-05 15:02:10
【问题描述】:
我正在使用Microsoft Office Interop Word 使用 C# 生成文档。为了使文档生成工作,"Microsoft Office Word 97 - 2003 Document" 下应该有一个 "Microsoft Office Word 97 - 2003 Document" 条目,如下所示:
首次安装 Microsoft Office 时,General Tab 下的 Local Path 路径正确。如果我随后将计算机加入域,然后以域用户重新启动系统,Local Path 将变为空白,并且应用程序不会生成文档并出现错误。
即使我先将计算机加入域,然后以域用户登录,然后安装 Microsoft Office,Local Path 首先显示正确,然后重新启动后再次变为空白。同时,如果我使用Local User 登录,Path 仍然存在。
是什么导致Local Path 的值变为空白?
所有这些设置都在虚拟机上进行,并且自动化一词适用于域帐户,因为我已经看到它在加入域的物理机上工作。
UPDATE: What my application is doing:
我的应用程序中有 4-5 个组件。
第一个是 VSTO Word AddIn,它与 Microsoft Word 集成,我们在其中创建包含一些表达式的新文档,这些表达式也保存在数据库中。表达式也有条件,它们也可以嵌套。表达式包含来自保存在数据库中的 XSD 文件的架构元素。一旦创建了这种类型的文档,它的 WordML 就会保存在数据库中。这一切都在 VSTO AddIn 中完成。
第二个是 Web 服务,它接收来自另一个组件的输入 xml,该组件向上面的 XSD 确认架构元素从该 XSD 嵌入到通过 VSTO addIn 创建的文档中的表达式中。此 Web 服务检查验证和其他几项任务。然后,它从数据库中获取相应 word 文档的 WordML,并将其传递给 Word Interop,后者使用其 API,递归地遍历它,以使用输入 xml 中的实际值替换模式元素。然后将 WordML 作为 word 文档保存到文件中。
这还会在保存之前将模板附加到文档。它还使用 Word Interop 的 SaveAs 功能将文件另存为 PDF。
更新: 我再次浏览了我的完整应用程序,并开始知道我们正在通过解析 Office Open XML 来做所有事情(例如,用于将输入提供给 word 文档),但我们使用 Word 自动化所做的唯一事情如下:
- 使用 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 的情况下完成吗?
【问题讨论】:
-
您是否遇到任何异常?可能类似于“检索具有 CLSID {xxxx-xxxx-...} 的组件的 COM 类工厂失败,因为以下错误:80070005”
-
我在损坏注册表之前遇到此异常,但在虚拟机上全新安装 Microsoft Office 后,如果此 LocalPath 为空白,我会收到“word 无法打开此文档模板”。
-
您正在开发什么样的应用程序?
-
@teenup 还有什么我可以帮你的吗?我认为您已经从可靠和/或官方来源获得了答案。
标签: c# .net office-interop com-interop dcom