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?

Upvotes: 0

Views: 24265

Answers (3)

Michael Hutter
Michael Hutter

Reputation: 1542

  • Right click on Solution (in Solution Explorer)
  • Manage NuGet Packages for Solution...
  • Search for "Office"
  • Click on "Microsoft.office.Interop.Excel"
  • Set Checkboxes of your project
  • Click on Install and Apply

Upvotes: 2

TankoTech Solutions
TankoTech Solutions

Reputation: 11

It happens sometimes.

  • Right click on your project name in "Solution Explorer"

  • Select "Add Reference"

  • At ".NET" tab scroll and find "Microsoft Excel 12.0 Object Library"

  • Select it and click on "OK" button.

That's all.

Upvotes: 1

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.

Upvotes: 4

Related Questions