none
Error Unable to cast COM object RRS feed

  • Question

  • Hello Everyone!

    Not sure why this happening. I do have an app that giving me (see below) error only on one machine/laptop. I`m trying to export from Azure table a record and open it in Excel table with my app.

    All machines have same version of Excel, so I`m using dll Ver 1.9.0.0 in the app as Reference (Copy to Local set to True).

    Any ideas why this is happening?

    

    Thank you.

    Alex


    Alex

    • Moved by Neda Zhang Wednesday, January 18, 2017 8:38 AM Moved from VB.NET
    Tuesday, January 17, 2017 10:32 PM

All replies

  • Hello,

    Can you get this to work on a machine that is not your development machine? 

    What does the code look like?


    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, January 18, 2017 12:30 AM
  • Yes, it works on multiple machines.

    The code is pretty straight forward:

    ''' <summary>
        ''' Exporting all data to Excel
        ''' </summary>
        Private Sub Export2Excel()
    
            xlApp = CreateObject("Excel.Application")
    
            Dim xlWorkBook As Excel.Workbook
            xlWorkBook = xlApp.Workbooks.Add
    
            '@@@@@@@@@@@@@@@@@@@@@@
            Dim k As Integer = 0
            Dim recordcount As Integer
    
            Dim accountname As String = My.Settings.Repository
            Dim accountkey As String = My.Settings.StorageKey
            Dim creds As StorageCredentials = New StorageCredentials(accountname, accountkey)
            Dim account As CloudStorageAccount = New CloudStorageAccount(creds, useHttps:=True)
            Dim client As CloudTableClient = account.CreateCloudTableClient()
            Dim table As CloudTable = client.GetTableReference(My.Settings.Table)
    
            Dim Query As TableQuery(Of CALLITEM) = New TableQuery(Of CALLITEM)().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.NotEqual, "PRODUCTMENU"))
    
            recordcount = table.ExecuteQuery(Query).Count
    
            'MsgBox(recordcount)
    
            Dim LogReport(recordcount, 10)
    
            ' Print all fields for record
            For Each record As CALLITEM In table.ExecuteQuery(Query)
    
                LogReport(k, 0) = record.PartitionKey ' Contact Name
                LogReport(k, 1) = record.RowKey ' Reference Number AKA row key.
                LogReport(k, 2) = record.ORDERNUMBER
                LogReport(k, 3) = record.PHONENUMBER
                LogReport(k, 4) = record.CALLTYPE
                LogReport(k, 5) = record.PRODUCTNAME
                LogReport(k, 6) = record.TERRITORY
                LogReport(k, 7) = record.DESCRIPTION
                LogReport(k, 8) = record.DATETIME
                LogReport(k, 9) = record.USERNAME
    
                k += 1
    
            Next record
    
            Dim lastrow As Integer
    
            With xlApp
                .Visible = True
    
                lastrow = .Range("A" & .Rows.Count).End(Excel.XlDirection.xlUp).Row
    
                '.Cells(1, 2) = " REPORT FOR " : .Cells(1, 3) = UserName.ToUpper : .Cells(1, 4) = " GENERATED ON : " & Date.Now
    
                Dim y As Integer = lastrow
    
                For k = 0 To recordcount
    
                    y += 1
    
                    .Cells(y, 1) = LogReport(k, 0) ' Name
                    .Cells(y, 2) = LogReport(k, 1) ' Reference
                    .Cells(y, 3) = LogReport(k, 2) ' OrderNumber
                    .Cells(y, 4) = LogReport(k, 3) ' Phone
                    .Cells(y, 5) = LogReport(k, 4) ' Calltype
                    .Cells(y, 6) = LogReport(k, 5) ' ProductName
                    .Cells(y, 7) = LogReport(k, 6) ' Territory
                    .Cells(y, 8) = LogReport(k, 7) ' Description
                    .Cells(y, 9) = LogReport(k, 8) ' Date
                    .Cells(y, 10) = LogReport(k, 9) ' UserName
    
                Next k
    
                .Columns("A:F").EntireColumn.AutoFit
    
            End With
    
            ' FORMATING
            With xlApp
                lastrow = .Range("A" & .Rows.Count).End(Excel.XlDirection.xlUp).Row
    
                ' formating the table
                .ActiveSheet.ListObjects.Add(Excel.XlListObjectSourceType.xlSrcRange, .Range("A1:J" & lastrow.ToString), Excel.XlYesNoGuess.xlNo).Name = "Table1"
                .Range("Table1[#All]").Select()
                .ActiveSheet.ListObjects("Table1").TableStyle = "TableStyleLight10"
    
                .ActiveSheet.ListObjects("Table1").HeaderRowRange(1, 1) = "NAME"
                .ActiveSheet.ListObjects("Table1").HeaderRowRange(1, 2) = "REFERENCE"
                .ActiveSheet.ListObjects("Table1").HeaderRowRange(1, 3) = "ORDER NUMBER"
                .ActiveSheet.ListObjects("Table1").HeaderRowRange(1, 4) = "PHONE"
                .ActiveSheet.ListObjects("Table1").HeaderRowRange(1, 5) = "CALL TYPE"
                .ActiveSheet.ListObjects("Table1").HeaderRowRange(1, 6) = "PRODUCT"
                .ActiveSheet.ListObjects("Table1").HeaderRowRange(1, 7) = "TERRITORY"
                .ActiveSheet.ListObjects("Table1").HeaderRowRange(1, 8) = "DESCRIPTION"
                .ActiveSheet.ListObjects("Table1").HeaderRowRange(1, 9) = "DATE"
                .ActiveSheet.ListObjects("Table1").HeaderRowRange(1, 10) = "LOGGED BY"
    
            End With
    
        End Sub


    Alex

    Wednesday, January 18, 2017 4:21 AM
  • Hi Alex,

    Thank you for posting here.

    Since this issue is more related to Excel, So I will move it to Excel for Developers forum for better support.

    Thanks for your understanding and support.

    Best Regards,

    Neda Zhang


    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 MSDNFSF@microsoft.com.

    Wednesday, January 18, 2017 8:38 AM
  • Hi,

    According to error message and your description, I think the issue doesn’t cause from the code.

    >>Ver 1.9.0.0

    Do you refer Microsoft Excel 16.0 Object Library?

    I would suggest you check if the dll exist and you could try to add Microsoft.Office.Interop.Excel as reference.

    Regards,

    Celeste


    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 MSDNFSF@microsoft.com.

    Thursday, January 19, 2017 7:59 AM
    Moderator
  • In the References I do have Object Library's. 

    I set the first one Copy Local.  If I go to Publish --> Application Files I can see that its including the library and I assume when app gets installed it will be referencing that particular dll.?

    At the beginning of my code I also do have a line :

    Imports Microsoft.Office.Interop
    

     

    Or am I doing smth wrong?

    Thank you.

    Alex.


    Alex

    Friday, January 20, 2017 3:11 PM
  • Hi,

    I think there is no need to set Copy Local into True. PIA is default installed when Office is installed.

    For more information, please visit: Office Primary Interop Assemblies and Designing and Creating Office Solutions

    What is your office version? 32bit or 64bit?

    Do you check what is the difference between this unworkable computer and others? Like Office version, Multi version Installation etc. 

    Besides, you could see the similar thread:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/6e859afd-53a9-4287-a1eb-9b331584ed18/error-loading-type-librarydll-microsoftofficeinteropexcelapplication?forum=visualstudiogeneral


    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 MSDNFSF@microsoft.com.

    Tuesday, January 24, 2017 7:24 AM
    Moderator