0

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?

Community
  • 1
  • 1
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

2 Answers2

4

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.

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • Bizarre why? `Office Interop` is not part of the basic .NET Framework included with Visual Studio and thus you have to add the corresponding library. When you write `Imports` at the top of your code pages, you are referring to already-installed namespaces. Your code was importing what wasn't present and that's why the error. But all this should be explained in the place where you got your code from. In any case, this is a quite basic issue (happening every time you write code relying on external dependencies). – varocarbas Oct 27 '15 at 22:03
  • That"s not what I'm saying is bizarre - that the References are not shown in the Solution Explorer, cannot be added and viewed from there, is what seems convoluted to me. – B. Clay Shannon-B. Crow Raven Oct 27 '15 at 22:05
  • What Visual Studio version are you using? – varocarbas Oct 27 '15 at 22:08
  • 1
    VB does not show all files by default. You need to click on the "Show All Files" button in the solution explorer. – TnTinMn Oct 27 '15 at 22:10
  • @varocarbas: VS 2013 Premium. – B. Clay Shannon-B. Crow Raven Oct 27 '15 at 22:11
  • @TnTinMn: That still does not show the Referenced DLL. – B. Clay Shannon-B. Crow Raven Oct 27 '15 at 22:12
  • 1
    I am using VS 2012 (and haven't changed any option) and you have various alternatives. In the solution explorer, you can right-click on the project name and, in the pop-up, click on "Add Reference". In the part below of this solution explorer window, there are two tabs "Solution Explorer" (the one currently selected) and "Class View"; if you select the second one, you would see the name of the application in a treeview, if you open it, you see "References". You can also go to the Project properties (back to "Solution Explorer", right click and "Properties") and select "References" on the LHS. – varocarbas Oct 27 '15 at 22:15
  • @varocarbas: That's interesting and helpful, thanks; still bizarre to me that the References folder isn't shown from the git-go. – B. Clay Shannon-B. Crow Raven Oct 27 '15 at 22:17
  • 1
    ... additionally, in the upper menu, under "PROJECT" you have also "Add reference", but I guess that this was the one you found. I think that both VB.NET & C# have exactly the same options. – varocarbas Oct 27 '15 at 22:17
1

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.