Creating an Excel Object using VB.NET

Hi everyone,

This is my problem:

I am trying to create an excel object so I can run an excel macro.
This is the code I a using:

Dim oExcelAs Excel.ApplicationClass

Dim oBookAs Excel.WorkbookClass

Dim oBooksAs Excel.Workbooks

Dim missingAsObject = System.Reflection.Missing.Value

Me.TextBox1.Text =Me.TextBox1.Text + " Report Creation in progress Please Wait..." + vbCrLf

'Start Excel and open the workbook.

oExcel =DirectCast(CreateObject("Excel.Application"), Excel.Application)

oExcel.Visible =False

oBooks = oExcel.Workbooks

oBook = oBooks.Open("C:\Developpement\Project_RCP\RCP_App\Job Bruts\Job RCP.xls", missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing)

'running the macros

oExcel.Run("MacroJob")

'Clean-up: Close the workbook and quit Excel.

Me.TextBox1.Text =Me.TextBox1.Text + " Report Successfully Created..." + vbCrLf

oBook.Close(False)

System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook)

oBook =Nothing

System.Runtime.InteropServices.Marshal.ReleaseComObject(oBooks)

oBooks =Nothing

oExcel.Quit()

System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel)

oExcel =Nothing

The red line gives me the following error message:

Cannot cast a COM object of type Microsoft.Office.Interop.Excel.ApplicationClass into a Classe Excel.ApplicationClass type .

The funny thing is this code does work on my PC where I wrote but it cannot run on my colleague's PC who is actually the end user.

Thank you for your help

[3015 byte] By [Intergore] at [2007-12-28]
# 1

Hi,

You would need to install the relevant .Net FRAMEWORK for your version

of Visual Studio on your colleagues PC.

Frameworks 1.1 for VisualStudio 2003,

Framework 2.0 ( and later ) for the 2005 version.

See this lot please.>>


Dim objExcel As New Microsoft.Office.Interop.Excel.Application

With objExcel
.Visible = True
.Workbooks.Open(My.Application.Info.DirectoryPath & "\PCSample.xls")
End With

See the url links in my answer here too please.>>

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1106530&SiteID=1
 
Regards,
S_DS
 
Spidermans_DarkSide at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic IDE...
# 2
Spiderman is correct...and you wil also have to have Excel installed on the client machine
DMan1 at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic IDE...
# 3
In order to work with an excel sheet, do you have to call a reference?
Diango at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic IDE...
# 4

Diango wrote:
In order to work with an excel sheet, do you have to call a reference?

Yes, you must make a project level reference to the Excel object library

DMan1 at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic IDE...
# 5

Yeah I do add references to the project. I add the COM Objects : Microsoft Office 11.0 Library
Microsoft Excel 11.0 Library and Microsoft word 11.0 Library as well.

I think my colleague doesn't have the .NET Framework so I will have to fix that. Thanks everyone for being so helpful, I really appreciate it.

Intergore at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic IDE...
# 6

this thread may help you: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=701557&SiteID=1

BTW. what is the version of office do you have installed?

Jack2005_MSFT at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic IDE...
# 7

Yeah I did make a project level reference and it worked fine.
Thank you all for your help

Intergore at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic IDE...