This project has moved. For the latest updates, please go here.

Addin for VBA IDE gives error on cast

Aug 17, 2013 at 10:22 PM
Hi All,

I was trying to use NetOffice to create a VBE addin that can be used in the VBA IDE (rather than Excel/PowerPoint etc.) However, when loading the addin (i.e. when it hits OnConnect), I receive the below error message:
System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to class type 'NetOffice.VBIDEApi.VBE'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.
I referenced VBIDEApi.dll and NetOffice.dll in my solution and am using .NET 2.0 and testing my solution against Office 2010 32-bit.

Here's my code for the OnConnection part, I've snipped the code to make it a bit more readable:
Imports NetOffice.VBIDEApi

Private _VBE As VBE

Public Sub OnConnection(ByVal application As Object, ByVal connectMode As NetOffice.Tools.ext_ConnectMode, ByVal addInInst As Object, ByRef custom As System.Array) Implements NetOffice.Tools.IDTExtensibility2.OnConnection

  Try
    _VBE = DirectCast(application, NetOffice.VBIDEApi.VBE)
  Catch ex As Exception
    MessageBox.Show(ex.ToString())
  End Try

End Sub
Please could anyone suggest anything I could try to get it to work? Just out of curiosity I changed the VBIDEApi.dll for Microsoft.Vbe.Interop.dll and it works fine - is there an issue with the VBIDEAPI.dll?

Thank you for any suggestions!
Coordinator
Aug 17, 2013 at 11:10 PM
NetOffice spend [Wrapper] assemblies.
please try this:
_VBE = new NetOffice.VBIDEApi.VBE(Nothing, application)

Sebastian
Aug 17, 2013 at 11:43 PM
Ahhh thank you!

I just changed the code to use your suggestion and it works perfectly! Sorry, I must read more of the documentation :)

Thank you for such a great library!