Naad Dyr
Naad Dyr

Reputation: 137

How to automate Microsoft Excel 2010 from Visual Basic .NET 2010

i have my database in VB2010 and i want to push 2 tables from that database to MS Excel 2010. I came across http://support.microsoft.com/kb/301982 but even after following the steps i'm getting an error: Type 'Excel.Application' is not defined.

Imports Microsoft.Office.Core
Imports Microsoft.Office.Interop.Excel
Imports System.Data

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
        Dim oXL As Excel.Application
        Dim oWB As Excel.Workbook
        Dim oSheet As Excel.Worksheet
        Dim oRng As Excel.Range
        '' etc...
    End Sub

End Class

Upvotes: 0

Views: 31920

Answers (4)

Check-Kay Wong
Check-Kay Wong

Reputation: 219

Here is an article. It summarises the use of Excel from VS2010. There is an exemple too create in VS2010. the sample works

http://checktechno.blogspot.com/2013/01/all-you-need-to-know-with-visual-basic.html

You also need to make sure the computer have Excel correctly installed. Do you have Excel 2010 Starter or the regular version?

As Tahbaza said, if the CreateObject fails, that means that something is wrong in that PC.

Upvotes: 0

Nadeer Madampat
Nadeer Madampat

Reputation: 330

Try adding reference from Menu: Project >> Add Reference >> .NET Tab

And add these:

Microsoft.Office.infoPath.Excel
Microsoft.Office.Tools.Excel

Upvotes: 3

Hans Passant
Hans Passant

Reputation: 941495

 Imports Microsoft.Office.Interop.Excel

That means that the type name you use is just plain Application, not Excel.Application. That's going to cause trouble though, that will be an ambiguous type name in a Winforms or WPF application, they also have a common type named Application. Which is why you so commonly see Excel.Application in sample code. With Excel being a namespace alias. Which you create like this:

 Imports Excel = Microsoft.Office.Interop.Excel

Now you can use Excel.Application without trouble.

VB.NET also permits this:

 Imports Microsoft.Office.Interop

But that doesn't work in C#, the namespace alias creates more portable code. Not sure if that's important at all.

Upvotes: 2

Tahbaza
Tahbaza

Reputation: 9548

Use of the direct Excel and other office automation libraries requires the full product to be installed on the machine running your code. Look into use NPOI or other Excel automation library to accomplish the same without having to install the office suite.

Installing Office on a server is usually not advisable, and even if you're running in an end client desktop environment you'd have to worry about the version of Office installed and other client configuration variables of the installation to get a successful execution every time.

If you insist on the path you've chosen initially, you'll need to capture a reference to either a currently running Excel application instance (GetObject) or create one for yourself to use (CreateObject), like so:

 Set oXL = CreateObject("Excel.Application")
 oXL.Visible = True

Upvotes: 1

Related Questions