VTV
asked on
System.InvalidCastException error
Hi all,
I am new to VB.NET, I want to open a excel file as below
Dim obj As Excel.Application
obj = CreateObject("Excel.Applic ation")// error here
obj.Visible = True
obj.Workbooks.Open("D:\PMO \VB.NET\Te st\Test\bi n\Test.xls ")
But error show up at line CreateObject...
An unhandled exception of type 'System.InvalidCastExcepti on' occurred in Try.exe
Additional information: Specified cast is not valid.
Please help me,
Thanks
I am new to VB.NET, I want to open a excel file as below
Dim obj As Excel.Application
obj = CreateObject("Excel.Applic
obj.Visible = True
obj.Workbooks.Open("D:\PMO
But error show up at line CreateObject...
An unhandled exception of type 'System.InvalidCastExcepti
Additional information: Specified cast is not valid.
Please help me,
Thanks
ASKER
Dear Ronald,
it still doesn't work now
it still doesn't work now
the reason is, you are actually trying combine 2 methods, late and early binding
it should be either
this (late binding)
Dim oExcel As Object
oExcel = CreateObject("Excel.Applic ation"), Excel.Application
Dim wbk As Object
wbk = oExcel.Workbooks.Open(File name:="C:\ temp\test. xls", UpdateLinks:=False, ReadOnly:=False)
With wbk.ActiveSheet
.Range("B1").Value = "Iouri" & .Range("A1").value
.Range("B2").Value = "Iouri" & .Range("A2").value
.Range("B3").Value = "Iouri" & .Range("A3").value
.PrintOut()
End With ' wbk
etc...
or this (early binding)
Dim oExcel As Excel.Application = New Excel.Application
Dim objWS As New Excel.Worksheet
ObjExcelWrite.Workbooks.Op en(WriteFi lepath)
ObjExcelWrite.Visible = False
objWS = ObjExcelWrite.Worksheets.I tem(1)
objWS.Cells.Clear()
etc...
it should be either
this (late binding)
Dim oExcel As Object
oExcel = CreateObject("Excel.Applic
Dim wbk As Object
wbk = oExcel.Workbooks.Open(File
With wbk.ActiveSheet
.Range("B1").Value = "Iouri" & .Range("A1").value
.Range("B2").Value = "Iouri" & .Range("A2").value
.Range("B3").Value = "Iouri" & .Range("A3").value
.PrintOut()
End With ' wbk
etc...
or this (early binding)
Dim oExcel As Excel.Application = New Excel.Application
Dim objWS As New Excel.Worksheet
ObjExcelWrite.Workbooks.Op
ObjExcelWrite.Visible = False
objWS = ObjExcelWrite.Worksheets.I
objWS.Cells.Clear()
etc...
sorry the last should be
Dim ObjExcelWrite As New Excel.Application
Dim objWS As New Excel.Worksheet
ObjExcelWrite.Workbooks.Op en(WriteFi lepath)
ObjExcelWrite.Visible = False
objWS = ObjExcelWrite.Worksheets.I tem(1)
objWS.Cells.Clear()
Dim ObjExcelWrite As New Excel.Application
Dim objWS As New Excel.Worksheet
ObjExcelWrite.Workbooks.Op
ObjExcelWrite.Visible = False
objWS = ObjExcelWrite.Worksheets.I
objWS.Cells.Clear()
ASKER
it is still not working, the fact that my code run well yesterday, but today it doesn't work anymore, I don't know why.
Both the methods I gave should work (I tested them)
Do you have excel installed on the computer you are testing this on.
The second method can't give you an invalidcast error so what is the error you get there
Do you have excel installed on the computer you are testing this on.
The second method can't give you an invalidcast error so what is the error you get there
ASKER
Yes, you are right, even my code run well on another PC, but I don't why it doesn't work on my PC. Maybe the problem is not syntax. If I can't fix this error, I must reinstall Window
that sounds a bit drastic, maybe the excel object is just corrupt
Maybe try this...
System.Diagnostics.Process .Start("D: \PMO\VB.NE T\Test\Tes t\bin\Test .xls")
I believe this will launch almost any type of known file...
The other thing to see is if D-Drive is mapped on the machines both the same way, and if they both have the same security privelages...
System.Diagnostics.Process
I believe this will launch almost any type of known file...
The other thing to see is if D-Drive is mapped on the machines both the same way, and if they both have the same security privelages...
ASKER
I removed office 2000 and install office 2003, with my code, another error show up
An unhandled exception of type 'System.Runtime.InteropSer vices.COME xception' occurred in mscorlib.dll
Additional information: Member not found.
P/S:I choose MS Excel 5.0 Object Library in item Project\Add Reference\COM tab
An unhandled exception of type 'System.Runtime.InteropSer
Additional information: Member not found.
P/S:I choose MS Excel 5.0 Object Library in item Project\Add Reference\COM tab
ASKER
ok, I changed MS Excel 5.0 Object Library to 11.0 and it run well now.
anyway thanks you guys alot
anyway thanks you guys alot
5.0, no wonder it didn't work, glad you got it solved :-)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Dim oExcel As Excel.Application = CType(CreateObject("Excel.