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?

2 Answers

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.

9

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest