添加对 Office 库的引用失败:类型或命名空间名称 Word 未找到
我正在尝试使用示例代码:
我转到“添加引用”对话框并添加“Microsoft Word 12 库”,但它没有出现在解决方案资源管理器中。我还添加了: using Microsoft.Office;
在尝试声明“Word”对象时收到以下错误消息。
错误 1:找不到类型或命名空间名称“Word”(是否缺少 using 指令或程序集引用?)
有关如何正确引用 Word 库的任何建议吗?
I'm trying to use the sample code here:
I went to "Add Reference" dialog and added "Microsoft Word 12 library", but it doesn't appear in the Solution Explorer. I also added: using Microsoft.Office;
I get the following error message when trying to declare a "Word" object.
Error 1: The type or namespace name 'Word' could not be found (are you missing a using directive or an assembly reference?)
Any suggestions on how to properly reference the Word library?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
编辑:更改为不使用剪贴板
Edit: changed so that it doesn't use the clipboard
您应该使用 Microsoft.Office.Interop.Word 程序集和指令
<代码>
使用 Microsoft.Office.Interop.Word;
You should use Microsoft.Office.Interop.Word assembly and directive
using Microsoft.Office.Interop.Word;
我添加了 Microsoft Word 对象库的引用,并在顶部编写了
using Microsoft.Office.Interop.Word;
。但代码不起作用。我正在使用此链接中给出的代码: http://support.microsoft.com/kb/316384< /a>然后,我将代码中的所有“Word”更改为“Microsoft.Office.Interop.Word”,并且它起作用了。
I added reference of microsoft word object library and also wrote
using Microsoft.Office.Interop.Word;
at the top. But the code didn't work. I was working with the code given in this link: http://support.microsoft.com/kb/316384Then, I changed all the "Word" with "Microsoft.Office.Interop.Word" inside the code and it worked.
您安装的是哪个版本的office?我的猜测是这是另一回事。您可以在此处查看官方名称到版本的映射:http://en.wikipedia.org/ wiki/Microsoft_Word#版本
which version of office do you have installed? My guess is it's a different one. You can see the mapping of official names to versions here: http://en.wikipedia.org/wiki/Microsoft_Word#Versions
您使用的技术要求您在开发人员计算机以及您要部署的任何计算机上安装特定版本的 Word。
自 Office 2007 起,Office 文档使用 Open XML 格式存储。可以使用 .NET Framework 中的打包和 XML API 读取和修改这些文档。您还可以使用 Open XML SDK 在打包和 XML API 之上提供更高级别的抽象层。使用此技术无需安装用于开发或生产用途的 Word。
有关使用 Open XML 的更多信息,请访问以下链接:
Open XML Developer
适用于 Microsoft Office 的开放 XML SDK 2.0
The technique you're using requires that you have a specific version of Word installed on your developer machine and on any machines you target for deployment.
Since Office 2007, Office documents are stored using Open XML formats. These documents can be read and modified using the Packaging and XML APIs in the .NET Framework. You can also use the Open XML SDK to give you a higher-level abstraction layer on top of the Packaging and XML APIs. Using this technique does not have Word installed for development or production use.
More information on working with Open XML can be found by following the links below:
Open XML Developer
Open XML SDK 2.0 for Microsoft Office
确保您已添加对 Microsoft.Office.Interop.Word 的引用。您可以通过右键单击引用来添加引用,选择“添加引用”,然后在“.NET”选项卡下查找 Microsoft.Office.Interop.Word
在程序文件中插入:
使用 Word = Microsoft.Office.Interop.Word;
注意“Word=”,这是解决你的痛苦的秘诀。顺便说一句,您必须安装 Word。
当我不确定用户是否安装了 Word 时,我的处理方法如下:
.
// 1. 此代码创建 Word 应用程序,这是操作 Word 文件的第一个必需步骤
源
Ensure you have added the reference to Microsoft.Office.Interop.Word. You can add the reference by right clicking on reference, choose "add reference" and then lookup for Microsoft.Office.Interop.Word under the ".NET" tab
In your program file insert:
using Word = Microsoft.Office.Interop.Word;
Notice the "Word=", this is the secret to solving your pain. You must have Word installed, btw.
Here's how I approach it, when I am not sure the user has Word installed:
.
// 1. This code creates the word application, a first REQUIRED step in manipulating a Word file
Source
用这个
代替
use this
instead of