469,642 Members | 1,103 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,642 developers. It's quick & easy.

Problem with Excel, Invalid Cast Exception Error

vinci
62
Please help me out... i only have two weeks to do it...


here is my code:

Expand|Select|Wrap|Line Numbers
  1.         Dim ex As New Object
  2.         Dim wb As Excel.Workbook
  3.         Dim sheet As Excel.Worksheet
  4.  
  5.         'create xls file and save into the specific path
  6.         ex = CreateObject("Excel.Application")
  7.  
  8.         Dim wrkbook As Excel.Workbook
  9.         wrkbook = ex.Workbooks.Open("C:\Inetpub\wwwroot\WWReport\Maxim.xls")
  10.         sheet = wrkbook.Sheets(3)
  11.  
the error is in line 9... in the above snippet

my program should be generating an excel report. i have a template, and when i open the template (see code) the error occurs...

why is it occuring? i used late binding to create my Excel.Application Object...

this code works fine in office 2003, unfortunately, the company where im in is using office 2002, i am using either Microsoft excel object Library 10.0 or Microsoft.office.interop.excel.dll (which i downloaded from the internet "PIA") but either way, error still occurs wether i use excel object 10.0 or PIA.

Working with VB.Net 2003, Microsoft office 2002 (excel)

pls. help me out!

tnx a lot!

THE ERROR!!!

-------------------------------------------------------------
Specified cast is not valid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Specified cast is not valid.

Source Error:


Line 60: 'open the xls file after it has been saved
Line 61: Dim wrkbook As Excel.Workbook
Line 62: wrkbook = ex.Workbooks.Open("C:\Inetpub\wwwroot\WWReport\Max im.xls") ->this line is the source of the error
Line 63: sheet = wrkbook.Sheets(3)
Line 64:


Source File: C:\Inetpub\wwwroot\WWReport\GWHM.aspx.vb Line: 62

Stack Trace:


[InvalidCastException: Specified cast is not valid.]
WWReport.GWHM.Page_Load(Object sender, EventArgs e) in C:\Inetpub\wwwroot\WWReport\GWHM.aspx.vb:62
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()
Aug 30 '07 #1
1 3873
vinci
62
just wanted to inform everyone that i already got the solution...

so ignore this thread! thanks!

what i did was, i declared all the variables as object (using the late binding method) and everything worked fine... how stupid of me to not immediately figure out the solution well in fact the soln is already in my hands...

Thank God!


TSDN Mabuhay!
Aug 30 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Walter Andosen | last post: by
2 posts views Thread by Darren Clark | last post: by
3 posts views Thread by John Howard | last post: by
1 post views Thread by Hifni Shahzard | last post: by
1 post views Thread by Boomessh | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.