使用 Interop 会在哪里失败?如果未安装 Office,请勿使用 Office.Interop。后期绑定?

Where will using Interop fail? Don't use Office.Interop if Office is not installed. late binding?

我的 windows 表单应用程序使用 Novacode DocX 从模板编写文档。项目的 Novacode 部分工作完美,文件保存。问题是,当我加载文档时,当 Novacode 部分添加标题等时,field/s(Table 的内容)不会更新。

我可以,也确实做到了,write a macro to update fields on open。这会解决问题,但并不是每个使用我的应用程序的人都会有这个宏。由于各种原因,我无法将文件另存为带有宏的 .docm 文件(假设文件必须是“.docx”)。

我发现 Microsoft.office.interop.Word 程序集允许我调用“Fields.Update”。我的理解是这可以解决问题,但我无法阻止未从 运行ning 我的应用程序安装 word 的用户。我的理解是,如果我是 "Using Microsoft.Office.Interop.Word",或者在我的参考资料中有它,如果有人没有消息,应用程序将不会 运行。

所以我有 code that checks if word is installed. If I run this, and it is installed, can I then use "Late Binding" to run interop code? Other related questions, have replies that point to "NetOffice" 作为 运行 互操作的方式,而不检查是否安装了 word。

我正在尝试通过我的研究使它尽可能全面。我的问题与这个非常相似“ how do I easily test the case where my C# application can't find an external assembly?”。我希望这个问题能尽快为大家解决,但我不确定是否会。

旁注,如果有人知道如何更新以“.docx”格式保存的 word 文档的字段,甚至只是现有的目录,而无需安装 word,那将是非常棒的,并且会绕过我的整个问题。虽然我仍然想知道互操作问题的答案。

这也是我在 Whosebug 上的第一个真正的问题,如果您有建议的标签,请在回答的同时提出。如果您对我提出问题的方式有任何反馈,我也会接受,但请不要 close/delete 没有任何答案的问题。我链接到类似的问题,但这些问题有一段时间没有得到答复。我相信我已经按照规则做了一切。

这更像是对您的 "if anyone knows a way to update the fields, or even just the existing TOC, of a word document that is saved in the ".docx" 格式的回答,没有安装 word" 问题,但您可能需要查看 Open XML SDK for Office.

这将允许您修改 .docx 文件,而无需安装 Word。

我发现 this tutorial 使用 Open XML SDK,我认为它几乎完全符合您的要求。

想说的很多,但我想我找到了答案

主要问题是如果我添加对 "Microsoft.Office.Interop.Word" 的引用并且客户端 运行 应用程序没有字, 哪里申请会失败?我现在的理解是,如果客户没有消息,它不会在启动时失败。但是,当到达使用 "Office.Interop.Word" 的代码时,它将失败。

防止这种情况的方法是简单的注册表检查方法。我使用了 This method to check the registry. Then before any of my code that uses the "Office.Interop.Word" code is run, I check if the client has word in the registry. If they don't have word, I take the proper notification actions for my application. I also surrounded the "Office.Interop.Word" code in a "try catch" exception block as a double safe measure. In my code the exception would mean word is not installed. A variation of the code using "Office.Interop.Word" I used to update fields can be found here 的变体。

Novacode DocX 可以支持 Docm 文件if you change the code yourself. I did not want to, and didn't use a docm file. Docm files have security warnings associated to them when emailed. So an auto updating macro是不可能的。

-章鱼表情符号正在庆祝