添加对 Office 库的引用失败:类型或命名空间名称 Word 未找到

发布于 2024-09-04 04:16:45 字数 434 浏览 7 评论 0原文

我正在尝试使用示例代码

我转到“添加引用”对话框并添加“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

夜血缘 2024-09-11 04:16:45

编辑:更改为不使用剪贴板

using Microsoft.Office.Interop.Word;

public string Test(string path)
{
    Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
    object file = path;
    object nullobj = System.Reflection.Missing.Value;

    Document doc = wordApp.Documents.Open(ref file, ref nullobj, ref nullobj,
                                           ref nullobj, ref nullobj, ref nullobj,
                                           ref nullobj, ref nullobj, ref nullobj,
                                           ref nullobj, ref nullobj, ref nullobj);

    string result = doc.Content.Text.Trim();
    doc.Close();
    return result;
}

Edit: changed so that it doesn't use the clipboard

using Microsoft.Office.Interop.Word;

public string Test(string path)
{
    Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
    object file = path;
    object nullobj = System.Reflection.Missing.Value;

    Document doc = wordApp.Documents.Open(ref file, ref nullobj, ref nullobj,
                                           ref nullobj, ref nullobj, ref nullobj,
                                           ref nullobj, ref nullobj, ref nullobj,
                                           ref nullobj, ref nullobj, ref nullobj);

    string result = doc.Content.Text.Trim();
    doc.Close();
    return result;
}
深陷 2024-09-11 04:16:45

您应该使用 Microsoft.Office.Interop.Word 程序集和指令
<代码>
使用 Microsoft.Office.Interop.Word;

You should use Microsoft.Office.Interop.Word assembly and directive

using Microsoft.Office.Interop.Word;

鱼忆七猫命九 2024-09-11 04:16:45

我添加了 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/316384

Then, I changed all the "Word" with "Microsoft.Office.Interop.Word" inside the code and it worked.

七秒鱼° 2024-09-11 04:16:45

您安装的是哪个版本的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

冷夜 2024-09-11 04:16:45

您使用的技术要求您在开发人员计算机以及您要部署的任何计算机上安装特定版本的 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

牛↙奶布丁 2024-09-11 04:16:45
  1. 确保您已添加对 Microsoft.Office.Interop.Word 的引用。您可以通过右键单击引用来添加引用,选择“添加引用”,然后在“.NET”选项卡下查找 Microsoft.Office.Interop.Word

  2. 在程序文件中插入:

    使用 Word = Microsoft.Office.Interop.Word;

    注意“Word=”,这是解决你的痛苦的秘诀。顺便说一句,您必须安装 Word。

    当我不确定用户是否安装了 Word 时,我的处理方法如下:
    .

// 1. 此代码创建 Word 应用程序,这是操作 Word 文件的第一个必需步骤

Word.ApplicationClass wordApplication;
try { wordApplication = new Word.ApplicationClass(); }
catch (Exception e) { MessageBox.Show("ERROR! Do you have MS Word installed? " + e.Message.ToString()); }

  1. 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

  2. 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

Word.ApplicationClass wordApplication;
try { wordApplication = new Word.ApplicationClass(); }
catch (Exception e) { MessageBox.Show("ERROR! Do you have MS Word installed? " + e.Message.ToString()); }

Source

陈甜 2024-09-11 04:16:45

用这个

using Excel = Microsoft.Office.Interop.Excel;

代替

using Microsoft.Office.Interop.Word; 

use this

using Excel = Microsoft.Office.Interop.Excel;

instead of

using Microsoft.Office.Interop.Word; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文