vb.net 无法识别 Microsoft.Office.Interop.Word 命名空间的别名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20911217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:19:46  来源:igfitidea点击:

Alias for Microsoft.Office.Interop.Word namespace not recogized

vb.netvisual-studionamespacesms-wordoffice-interop

提问by John Adams

Here is my code in Form1.vb:

这是我在 Form1.vb 中的代码:

Imports Word = Microsoft.Office.Interop.Word

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, _
  ByVal e As System.EventArgs) Handles Button1.Click

    Dim oWord As Word.Application
    Dim oWord1 As Microsoft.Office.Interop.Word.Application
    Dim oWord2 as Application

I'm using VStudio2012 and Office 2010 and following the sample from https://support.microsoft.com/kb/316383

我正在使用 VStudio2012 和 Office 2010 并遵循https://support.microsoft.com/kb/316383 中的示例

The declaration of oWordis flagged by intellisense as "Type 'Word.Application' is not defined,
while oWord1and oWord2are accepted. Intellisense provides several error correction options - two of which I've tried (oWord1 and oWord2) - the other options don't seem to apply.

oWord的声明被Intellisense标记为“类型 'Word.Application' 未定义,
oWord1oWord2被接受。Intellisense提供了几个纠错选项 - 其中两个我已经尝试过(oWord1 和 oWord2) - 其他选项好像不适用。

In short, I am confused about why the use of the namespace alias Word on the Imports statement does not satisfy the syntax Dim oWord As Word.Applicationas described in the KB article. Is there some "master knob" to turn in the project properties dialog or something?

简而言之,我很困惑为什么在 Imports 语句上使用命名空间别名 Word 不满足知识库文章中描述的语法Dim oWord As Word.Application。是否有一些“主旋钮”可以打开项目属性对话框或其他东西?

回答by laylarenee

You will need to add a reference to import the DLL for this namespace. In Visual Studio 2012, in your solution explorer` right click on the project name > click "Add Reference" > Choose "Assemblies" > "Extensions" > "Microsoft.Office.Interop.Word". There may be multiple version of the Interop Libraries (Office 2003, 2007, 2010, etc), choose the appropriate one.

您将需要添加一个引用以导入此命名空间的 DLL。在Visual Studio 2012解决方案资源管理器中,右键单击项目名称> 单击“添加引用”> 选择“程序集”>“扩展”>“Microsoft.Office.Interop.Word”。可能有多个版本的互操作库(Office 2003、2007、2010 等),选择合适的一个。

I believe these assemblies are originally installed when you do your installation of Microsoft Office from the DVD. So you will need to have Word installed I believe.

我相信这些程序集最初是在您从 DVD 安装 Microsoft Office 时安装的。所以我相信你需要安装Word。

Edit: this works for me, with Intellisense:

编辑:这对我有用,使用 Intellisense:

Imports Word = Microsoft.Office.Interop.Word
Module Module1
    Sub Main()
        Dim oWord As Word.Application
    End Sub
End Module

回答by Joe

Instead of 'Imports Word = Microsoft.Office.Interop.Word' I used 'Imports Microsoft.Office.Interop' and it worked fine

我使用了“导入 Microsoft.Office.Interop”而不是“导入 Word = Microsoft.Office.Interop.Word”,它运行良好