Share via


System.InvalidCastException when creating an excel document

Question

Friday, June 7, 2019 7:58 PM

I am writing an application that reads some data from an AutoCAD DWG file then exports it into an excel spreadsheet. Everything works fine on my dev computer, however, when i run it on a client machine i get a System.InvalidCastException

https://drive.google.com/file/d/15an9L9KU-Nuxhp--1Uz_XhJmwHB9U4Hw/view?usp=sharing

The target framework is .NET 4.6.1, and the target machine has Excel installed as part of the MS Office 365 subscription.

    Dim xlApp As Excel.Application
    Dim xlWorkBook As Excel.Workbook
    Dim xlWorkSheet As Excel.Worksheet
    Dim misValue As Object = System.Reflection.Missing.Value

    xlApp = New Excel.Application
    Try
      xlWorkBook = xlApp.Workbooks.Add(misValue) ' error on this line
    Catch ex As Exception
      MsgBox(ex.ToString())
      MsgBox(ex.InnerException())
      RaiseEvent finished()
      Exit Sub
    End Try

All replies (8)

Friday, June 7, 2019 8:48 PM

Oh this can be a tricky one e.g. if you added Excel as a COM reference vs a DLL reference, could be different versions of Excel between machines, could be how you are adding the WorkBook.

You could look at my Excel code sample on MSDN for creating Excel file  using automation or look at SpreadSheetLight which is totally free, no cost, requires about three lines of code to create an Excel file, simple methods to set cell values and styles. 

My recommendation is verify what I mentioned in the first paragraph first as this may resolve the issue once you get thru the list.

Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

NuGet BaseConnectionLibrary for database connections.

StackOverFlow


Monday, June 10, 2019 9:20 AM

Hi,

You can see if it is the same issue,changed the target from x86 to x64.

https://social.msdn.microsoft.com/Forums/office/en-US/4ea538c3-1814-4260-a65d-208adb3161a1/how-to-solve-8220unable-to-cast-com-object-of-type?forum=accessdev

Best Regards,

Alex

MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].


Tuesday, June 11, 2019 3:11 PM

I have added them as COM objects because it is for a *.DLL project and as such can't add references as dll's. I looked into SpreadSheetLight and didnt have much luck getting it working.


Tuesday, June 11, 2019 3:40 PM

I have added them as COM objects because it is for a *.DLL project and as such can't add references as dll's. I looked into SpreadSheetLight and didnt have much luck getting it working.

What happened with SpreadSheetLight, I've used it for years with zero issues.

Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

NuGet BaseConnectionLibrary for database connections.

StackOverFlow


Tuesday, June 11, 2019 3:41 PM

I have added them as COM objects because it is for a *.DLL project and as such can't add references as dll's.

Why is that, DLL projects can use DLL references no problem.

Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

NuGet BaseConnectionLibrary for database connections.

StackOverFlow


Tuesday, June 11, 2019 6:09 PM

I moved the solution to x64 setup and i am still getting the same issue.


Wednesday, June 12, 2019 5:57 PM

The issue appears to be caused by an improper uninstall of the previous version of Excel installed on the client machine.


Friday, June 14, 2019 5:18 AM

The issue appears to be caused by an improper uninstall of the previous version of Excel installed on the client machine.

reinstall the latest excel can solve your problem?

Best Regards,

Alex

MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].