Link to home
Start Free TrialLog in
Avatar of VTV
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.Application")// error here
        obj.Visible = True
        obj.Workbooks.Open("D:\PMO\VB.NET\Test\Test\bin\Test.xls")
But error show up at line CreateObject...
An unhandled exception of type 'System.InvalidCastException' occurred in Try.exe

Additional information: Specified cast is not valid.
Please help me,
Thanks
Avatar of RonaldBiemans
RonaldBiemans

try it like this

Dim oExcel As Excel.Application = CType(CreateObject("Excel.Application"), Excel.Application)
Avatar of VTV

ASKER

Dear Ronald,
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.Application"), Excel.Application
       Dim wbk As Object

        wbk = oExcel.Workbooks.Open(Filename:="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.Open(WriteFilepath)
            ObjExcelWrite.Visible = False
            objWS = ObjExcelWrite.Worksheets.Item(1)
            objWS.Cells.Clear()

etc...
sorry the last should be
  Dim ObjExcelWrite As New Excel.Application
  Dim objWS As New Excel.Worksheet
         
            ObjExcelWrite.Workbooks.Open(WriteFilepath)
            ObjExcelWrite.Visible = False
            objWS = ObjExcelWrite.Worksheets.Item(1)
            objWS.Cells.Clear()
Avatar of VTV

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
Avatar of VTV

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.NET\Test\Test\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...
Avatar of VTV

ASKER

I removed office 2000 and install office 2003, with my code, another error show up
An unhandled exception of type 'System.Runtime.InteropServices.COMException' 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
Avatar of VTV

ASKER

ok, I changed MS Excel 5.0 Object Library to 11.0 and it run well now.
anyway thanks you guys alot
5.0, no wonder it didn't work, glad you got it solved :-)
ASKER CERTIFIED SOLUTION
Avatar of PAQ_Man
PAQ_Man
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial