简体   繁体   中英

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

I've got this code:

Imports Excel = Microsoft.Office.Interop.Excel

Public Class 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).

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

在此处输入图片说明

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM