none
Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.MailItem' ? RRS feed

  • Question

  • Hi,

    I have a button on custom addIn, If i click on this button then giving an error like "Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.MailItem'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063034-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)). "

    This is the create custom group with button. ( addIn)

    <?xml version="1.0" encoding="UTF-8"?>
    <customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="Ribbon_Load">
      <ribbon>
        <tabs>
          <tab idMso="TabMail">
            <group label="TWDM" getVisible="TWDMEmailExport_GetVisible" insertAfterMso="GroupMailNew" id="TWDMEmailExportGroup" >
              <button id="TWDMEmailExport"
                      size="large"
                      label="TWDM Email Export"                   
                      imageMso="FilePrepareMenu"
                      screentip="TWDM-Task Workflow Document Management System"
                      supertip="TWDM-Task Workflow Document Management System"                   
                      onAction="OnTWDMEmailExportClick"/>
            </group>
          </tab>
        </tabs>
      </ribbon>
    </customUI>
    

    I have selected more than 25 mails from outlook and click on button.

     ' OnTWDMEmailExportClick  routine handles  the button click event
     

     Public Sub OnTWDMEmailExportClick(ByVal control As Office.IRibbonControl)
            Try
                Dim msg As String = String.Empty
    
                If TypeOf control.Context Is Outlook.Explorer Then
                    ShowSelectedEmailsScreen(control.Context)
                Else
                    MessageBoxExn.ShowBox(_errorOccurred, _TWDMError, MessageBoxExn.XMessageBoxButtons.OK, MessageBoxExn.XMessageBoxIcon.Error)
                End If
    
            Catch ex As Exception
              
            End Try
        End Sub

    Private Sub ShowSelectedEmailsScreen(ByVal context As Object) Dim showSelectedEmails As New ShowSelectedEmails(TWDMEmailSettings) Dim count As Integer = 0 Try Dim explorer As Outlook.Explorer = TryCast(context, Outlook.Explorer) Dim selection As Outlook.Selection = explorer.Selection For Each selectedItem As Outlook.MailItem In selection

    ' Storing in to object and showing in to grid..

    Next ' Here error is getting for 15th item. Catch ex As Exception End If End Try

    I searched this issue in this forum there i found  the application should have "Microsoft.Office.Interop.Outlook - 14.0.0.0 " . So i updated to 14.0.0.0, even though it's giving same error.

    I am using Outlook 2010 , Outlook 2007 Addin template in VS 2008.

    I hope that I have posted my question in the correct forum.

    Please  help me to do solve this..

    Thanks in advance..



    • Edited by Narsa Monday, August 19, 2013 8:03 AM
    Monday, August 19, 2013 7:54 AM

Answers

  • You are assuming that you can only have MailItem object in the folder. In case of the Inbox, you can also have NDRs (ReportItem), meeting requests/updates (MeetingItem), etc.

    Instead of

    For Each selectedItem As Outlook.MailItem In selection

    use

    For Each selectedItem As Object In selection

    and retrieve the Type and/Message class property dynamically first to figure out the item's type.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    • Marked as answer by Narsa Monday, August 19, 2013 3:08 PM
    Monday, August 19, 2013 1:41 PM

All replies

  • You are assuming that you can only have MailItem object in the folder. In case of the Inbox, you can also have NDRs (ReportItem), meeting requests/updates (MeetingItem), etc.

    Instead of

    For Each selectedItem As Outlook.MailItem In selection

    use

    For Each selectedItem As Object In selection

    and retrieve the Type and/Message class property dynamically first to figure out the item's type.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    • Marked as answer by Narsa Monday, August 19, 2013 3:08 PM
    Monday, August 19, 2013 1:41 PM
  • Thank you for quick reply. Now i able to get only mail item from object.
    Monday, August 19, 2013 3:09 PM