472,798 Members | 1,304 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,798 software developers and data experts.

Problem with Excel, Invalid Cast Exception Error

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
  5.         'create xls file and save into the specific path
  6.         ex = CreateObject("Excel.Application")
  8.         Dim wrkbook As Excel.Workbook
  9.         wrkbook = ex.Workbooks.Open("C:\Inetpub\wwwroot\WWReport\Maxim.xls")
  10.         sheet = wrkbook.Sheets(3)
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!


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)
Aug 30 '07 #1
1 4320
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

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

Similar topics

by: Paul | last post by:
I was wondering if anyone has had an issue where using vba code to read an excel file and import the data into an access table some records are not imported from the excel file. It seems looking at...
by: Walter Andosen | last post by:
I have some trouble creating a Excel workbook. I get lots of Exceptions. For example I get System.Runtime.InteropServices.COMException (0x80028018): Old format or invalid type library. at...
by: Darren Clark | last post by:
I am having some troubles with Sessin varables dieing.. Basically ihave my own class structure called USER that once the user logs in i save it to a session var called Session = myUserObject; ...
by: John Howard | last post by:
Making the following call to a local MSAccess database works fine: Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) Dim intRows As Integer Dim strSQL As String Dim ds As New...
by: Hifni Shahzard | last post by:
Hi, I got a stored procedure, where it returns a value. But if I execute it. It gives an error as "Invalid cast from System.Int32 to System.Byte.". To make clear how do I execute this, below I'm...
by: Boomessh | last post by:
Hi, I am trying to open an existing excel workbook. It pops up an error stating that “Errors were detected in 1598.xls but Microsoft Office Excel was able to open the file by making the repairs...
by: Brano | last post by:
Hi all, I have a asp.net website that has been live for about 2 weeks now and there were no problems with it. I have got a new server that is Win 2003 IIS 6.0 I have moved my application onto...
by: Bill Fallon | last post by:
I am developing a VB.Net application with VS 2005 that opens an Excel workbook and populates the worksheet with data. I started developing the application with Office 2007 installed on my Vista...
by: John Heitmuller. | last post by:
Hi, I have a VB.Net application that I have recently upgraded from Visual Studio 2003 to Visual Studio 2005. The VB.Net app accesses Word and Excel. When I run the newly compiled VB.Net app on...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
by: lllomh | last post by:
How does React native implement an English player?
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.