[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 353
  • Last Modified:

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
0
VTV
Asked:
VTV
1 Solution
 
RonaldBiemansCommented:
try it like this

Dim oExcel As Excel.Application = CType(CreateObject("Excel.Application"), Excel.Application)
0
 
VTVAuthor Commented:
Dear Ronald,
it still doesn't work now
0
 
RonaldBiemansCommented:
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...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
RonaldBiemansCommented:
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()
0
 
VTVAuthor Commented:
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.
0
 
RonaldBiemansCommented:
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
0
 
VTVAuthor Commented:
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
0
 
RonaldBiemansCommented:
that sounds a bit drastic, maybe the excel object is just corrupt
0
 
natlozCommented:
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...
0
 
VTVAuthor Commented:
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
0
 
VTVAuthor Commented:
ok, I changed MS Excel 5.0 Object Library to 11.0 and it run well now.
anyway thanks you guys alot
0
 
RonaldBiemansCommented:
5.0, no wonder it didn't work, glad you got it solved :-)
0
 
PAQ_ManCommented:
PAQed with points refunded (125)

PAQ_Man
Community Support Moderator
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now