Search code examples
vb.netexcel-interopexcel.application

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:

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?


Solution

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