Strange Issue with Excel Interop

I have recently moved to 2005 Studio Standard edition and after some fighting initally with my laptop and it's issues with VS2005 and left over beta bits and so on, I have totally rebuilt my machine.

In short = fresh copy of xp pro and VS2005 std and Office 2003

IIn VB 2003 I used to be able to add a reference to the Excel 11.0 library and then use the following line:

Imports Microsoft.Office.Interop

However that is not possible any more. Following the same steps I can only import the following:

Imports Microsoft.Office.Core

This presents a problem if I want to do this, because the Interop is not available:

Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

So the Excel. anything is not defined

I have found some OfficeXP development stuff on Microsoft's website, which basically is all the interops available for office. Installed without problems and if I include the reference to Microsoft.Office.Interop.Excel the I can use the above mentioned code.

However if I try to use this

xlBook.SaveAs("C:\test.xls")

I get this error:

System.Runtime.InteropServices.COMException was unhandled
  ErrorCode=-2147417851
  Message="The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))"
  Source="Interop.Excel"

I have no idea what is going on and any help will be appreciated

Here is the whole code:

Imports System.IO
Imports Microsoft.Office.Interop

Public Class Form1

Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
OpenExcel()
PlayTime()
CloseExcel()
Debug.WriteLine(
"DONE")
End Sub

Sub OpenExcel()
xlApp =
CType(CreateObject("Excel.Application"), Excel.Application)
End Sub

Sub CloseExcel()
xlApp.Quit()
xlApp =
Nothing
End Sub

Sub PlayTime()
If File.Exists("C:\test.xls") Then
File.Delete("C:\test.xls")
End If

xlBook = xlApp.Workbooks.Add()
xlSheet =
CType(xlBook.Worksheets(1), Excel.Worksheet)
xlSheet.Name =
"Iggy"
xlBook.SaveAs("C:\test.xls")
End Sub

End Class



Answer this question

Strange Issue with Excel Interop

  • gicio

    Hi, I have exactly problem when I converted my code to vs 2005.
    So could you tell me what "quick Add Features" you are talking about Do you have the link for it

    Thanks

  • shayc

    Well, this was a case of self inflicted wounds...
    For one, the whole .Core issue was because the default installation of Office does NOT include the .NET Offce programability, so a quick Add Features install fixed that.

    The other issue with the SaveAs method was caused my the fact that I also had an installation of Office 2000 along with Office 2003. The reason behind that was that I didn't want to use late binding so I thought that I could use the 9.0 Library without consequences if I installed both versions on my system.....Not a good idea...

    So all in all I am back up and running... with a few lessons learned

  • Olivier Jooris

    Hi,

    Thanks for answering your own question. I am facing the same problem as you and I was wandering what add features you installed to fix it. I'm using Office 2003.

    cheers,

    Chris


  • Omid Bayani

    hi friends..am very very new to VS2005..n am facing the same problem as mentioned way above !! :(

    myt get a nervous breakdown if not helped soon ;)

    what am trying to do is save an xslx report(yes, am wrking in office 2007!!) to a path i specify as follows:

    xlBook.SaveAs("C://ChevronDocs//test.xlsx", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal,null,null,false,false,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared,false,false,null,null);

    n whoa !! i get error eror n error !! uff..

    please help..(i have specified the default path in web.config file.



  • Anson Horton

    Thank. That helps.

    gao

  • Paul Laudeman

    If i remember this correctly, if you go to add/remove features from office and after you expand Excel, you should see .NET Programmability Support.

    That is what I am talking about.

    Hope it helps


  • Strange Issue with Excel Interop