为什么即使在导入 Microsoft.Office.Interop.Excel (VB.NET) 后,所有 Excel 对象上仍然“未定义”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33378965/
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 19:32:08  来源:igfitidea点击:

Why do I get "not defined" on all the Excel objects even after importing Microsoft.Office.Interop.Excel (VB.NET)?

vb.netexcel-interopexcel.application

提问by B. Clay Shannon

I've got this code:

我有这个代码:

Imports Excel = Microsoft.Office.Interop.Excel

导入 Excel = Microsoft.Office.Interop.Excel

Public Class FormExcelTest_VB

公共类 FormExcelTest_VB

Private Sub ButtonCreateExcelFile_Click( sender As Object,  e As EventArgs) Handles ButtonCreateExcelFile.Click
    Dim xlApp As New Excel.Application
    Dim xlWorkBook As Excel.Workbook
    Dim xlWorkSheet As Excel.Worksheet
    . . .

I got it from here.

我从这里得到的。

It doesn't compile, though; I get:

但是,它不会编译;我得到:

Type 'Excel.Application' is not defined.
Type 'Excel.Workbook' is not defined.
Type 'Excel.Worksheet' is not defined.

Being unfamiliar with VB[.NET], I thought to myself, "Self, you probably forgot to add a required reference." But then I saw that there is no "References" folder in Solution Explorer for the project (coming from the land of C#, that seems awfully strange to me).

由于不熟悉 VB[.NET],我心里想,“ Self,你可能忘了添加一个必需的引用。”但后来我看到解决方案资源管理器中没有项目的“引用”文件夹(来自土地的 C#,这对我来说似乎很奇怪)。

The "helpful" msgs I get when hovering over the rejected code are:

将鼠标悬停在被拒绝的代码上时,我收到的“有用”消息是:

enter image description here

在此处输入图片说明

The first one doesn't seem the likely remedy, and the following ones even less so. How am I supposed to know how to resolve (no pun intended) these undefined types?

第一个似乎不是可能的补救措施,以下几个更是如此。我怎么知道如何解决(没有双关语意)这些未定义的类型?

回答by B. Clay Shannon

This still seems bizarre to me, but I discovered that you add references via Project > Add Reference...

这对我来说仍然很奇怪,但我发现您通过项目 > 添加引用添加引用...

I added "Microsoft Excel 12.0 Object Library" and now it compiles.

我添加了“Microsoft Excel 12.0 对象库”,现在可以编译了。

And View > Object Browser is the path to see which References you have added, I guess; the C# way seems far "friendlier" to me.

并且 View > Object Browser 是查看您添加了哪些 References 的路径,我猜;C# 方式对我来说似乎更“友好”。