none
Unable to cast COM object of type 'System.__ComObject' to class type RRS feed

  • Question

  • hello all,

    My project is in VS 2013 for Office 2013 in vb.net and connects to a vendors assembly dll to get exposure to their dialog boxes. My code then calls the vendors dialog.
    When I debug the code I get the error

    Unable to cast COM object of type 'System.__ComObject' to class type 'NetDocuments.Client.Common.Infrastructure.NdOfficeAddInUtilities'. 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.


    on line


    ndDialog = addin.Object


    I first wrote the code in vba and it successfully works (easier to  debug etc, & work as proof of concept to get it working. as vb.net solution is HUGE). Then convert vba code into vb.net and now it errors

    This is one function, however the same code is used for Word, Excel and PowerPoint.


    Many thanks in advance

    --dd


    --- vba code working

    Running in Word 2013 word template.dotm
    in Tools, References...add Vendors dll and save


    Sub TestSaveDialog()

        'ActiveDocument.Save

        Dim addIn As COMAddIn
        Dim automationObject As NetDocuments_Client_Common.INdOfficeAddInUtilities
        'use WordAddIn, PowerPointAddIn, ExcelAddIn values

        Set addIn = Application.COMAddIns("NetDocuments.Client.WordAddIn")
        Set automationObject = addIn.Object

        Dim profile As NetDocuments_Client_Common.NdProfileAttributesCollection
        Set profile = New NetDocuments_Client_Common.NdProfileAttributesCollection

        Dim strDocName As String
        strDocName = " "

        
        '' default to Home
        automationObject.ShowSaveDialog , profile, strDocName, "", "", ""

    End Sub

    --- vb.net code

     ''' <remarks>MS Word</remarks>
            Public Function SAPISaveDocAs(ByVal worddocument As WordDocument) As Boolean
    
                Try
    
                    If worddocument Is Nothing Then
                        Throw New ArgumentNullException("WordDocument passed was null")
                    End If
    
                    Dim ndo As Object
                    ndo = CreateObject("ndOffice.EchoingDataService")
                    Dim docInfo As Object
                    docInfo = ndo.getDocumentInfo(worddocument.Document.Application.ActiveDocument.FullName)
    
                    Dim MSWord As Microsoft.Office.Interop.Word.Application
                    MSWord = CType(GetObject(, "Word.Application"), Microsoft.Office.Interop.Word.Application)
                    MSWord.Visible = True
    
                    Dim addin As Microsoft.Office.Core.COMAddIn = MSWord.COMAddIns.Item
    
    ("NetDocuments.Client.WordAddIn")
    
                    Dim ndDialog As NetDocuments.Client.Common.Infrastructure.NdOfficeAddInUtilities
                    ndDialog = addin.Object
    
                    '' If document not saved in NetDocuments
                    If docInfo Is Nothing Then
    
                        If worddocument.Document.Application.Visible = True Then
    
                            '' default to nd Home
                            ndDialog.ShowSaveDialog(, , "", "", "")
    
                        End If
    
                    End If
    
    
                Catch ex As Exception
                    Logger.Log(Me.GetType(), ex)
                End Try
    
    
            End Function



    • Edited by dd_ Wednesday, June 17, 2015 6:24 AM
    Wednesday, June 17, 2015 6:11 AM

Answers

  • Hi,

    Based on my test, the COMAddIn.Object in VB is an windows runtime type. When I use the object type, the code works well for me.

    Here is the code for your reference:

      Dim addinObject As Object
    
                 addinObject = app.COMAddIns.Item("ExcelAddin1").Object
            addinObject.ImportData()
    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, June 18, 2015 3:38 AM
    Moderator

All replies

  • Hi,

    did you try to cast Object explicit?

    Dim ndDialog As NetDocuments.Client.Common.Infrastructure.NdOfficeAddInUtilities = DirectCast(addin.Object, NetDocuments.Client.Common.Infrastructure.NdOfficeAddInUtilities)
    
    
    


    Regards

    Wednesday, June 17, 2015 10:05 AM
  • Thanks - no I havent Ill give it a go!
    Wednesday, June 17, 2015 11:13 PM
  • Hi,

    Based on my test, the COMAddIn.Object in VB is an windows runtime type. When I use the object type, the code works well for me.

    Here is the code for your reference:

      Dim addinObject As Object
    
                 addinObject = app.COMAddIns.Item("ExcelAddin1").Object
            addinObject.ImportData()
    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, June 18, 2015 3:38 AM
    Moderator