0

I am trying to communicate with Excel through Visual Studio, using the Office Developer Tools, but cannot seem to cast the System._comobject objects to the Microsoft.Office.Tools.Excel types:

Private Sub ThisAddIn_Startup() Handles Me.Startup
    Me.Application.Workbooks.Add()
    Me.Application.Worksheets.Add()
    Dim sheet2 = Me.Application.Worksheets.Item(1)
    Dim cell = sheet2.range("A2")
    cell.Value = sheet2.GetType.ToString()
    Dim tmpSheet As Microsoft.Office.Tools.Excel.Worksheet = Me.Application.Worksheets.Item(1)
End Sub

When I run this code, the cell A2 contains "System._ComObject" But I get an exception when casting it to the Worksheet type. The exception is:

Unable to cast COM object of type 'System.__ComObject' to interface
type 'Microsoft.Office.Tools.Excel.Worksheet'. This operation failed
because the QueryInterface call on the COM component for the interface with
IID '{297DC8D9-EABD-45A1-BDEF-68AB67E5C3C3}' failed due to the following
error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

I am aware that this is often due to incorrect registery keys, like here: unable to cast COM object of type 'microsoft.Office.Interop.Excel.ApplicationClass' to 'microsoft.Office.Interop.Excel.Application'"

But I only have one registery key, removed it, and office itself put it back in it's place, so I guess it's the correct one.

Does anyone know how I can solve this? I use Visual Studio 2015, and office 2013 Standard, in english. Thank you in advance!

Community
  • 1
  • 1
DrDonut
  • 864
  • 14
  • 26

1 Answers1

0

Apperantly I needed to use the type:

Microsoft.Office.Interop.Excel.Worksheet

Instead of

Microsoft.Office.Tools.Excel.Worksheet
DrDonut
  • 864
  • 14
  • 26