none
Outlook The property "HasAttachments" is unknown. RRS feed

  • Question

  • Hi guys, 

    I'm trying to iterate thru all the emails in inbox only with the one that have attachments

    I'm using the Restrict Function to filter on attachment only. However I'm getting HasAttachments is unknown error

    Can you please assist?

        Sub SaveAllAttachmentsInInbox()
            Try
                Dim ns As Outlook._NameSpace = Nothing
                Dim inbox As Outlook.Folder = Nothing
                ns = OutlookApp.Session
                'inbox = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderJunk)
                inbox = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
    
                Dim mailItems As Outlook.Items
                Dim attachmentItems As Outlook.Items
                mailItems = inbox.Items
                attachmentItems = mailItems.Restrict("[HasAttachments]=true")
    
    
                For i As Integer = 1 To attachmentItems.Count
                    Dim outlookItem As Object = attachmentItems(i)
                    Dim mailItem As Outlook._MailItem = TryCast(outlookItem, Outlook._MailItem)
                    If mailItem IsNot Nothing Then
                        Dim attachments As Outlook.Attachments = mailItem.Attachments
                        While attachments.Count > 0
                            Dim attach As Outlook.Attachment = Nothing
                            attach = attachments(1)
                            attach.SaveAsFile("C:\Temp\Junk\" & attach.FileName)
                            MessageBox.Show("About to delete!")
                            attach.Delete()
                            Marshal.ReleaseComObject(attach)
                        End While
                        Marshal.ReleaseComObject(attachments)
                        mailItem.Save()
                    End If
                    Marshal.ReleaseComObject(outlookItem)
                Next
    
                'Release COM Objects
                If attachmentItems IsNot Nothing Then Marshal.ReleaseComObject(attachmentItems)
                If mailItems IsNot Nothing Then Marshal.ReleaseComObject(mailItems)
                If ns IsNot Nothing Then Marshal.ReleaseComObject(ns)
                If inbox IsNot Nothing Then Marshal.ReleaseComObject(inbox)
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                'Finally
                '    If attachmentItems IsNot Nothing Then Marshal.ReleaseComObject(attachmentItems)
                '    If mailItems IsNot Nothing Then Marshal.ReleaseComObject(mailItems)
                '    If ns IsNot Nothing Then Marshal.ReleaseComObject(ns)
                '    If inbox IsNot Nothing Then Marshal.ReleaseComObject(inbox)
            End Try
    
        End Sub

     


    Codernater

    Tuesday, July 7, 2015 7:24 PM

Answers

  • Use the @SQL notation:

    attachmentItems = mailItems.Restrict("@SQL=""urn:schemas:httpmail:hasattachment""=1");


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

    • Proposed as answer by L.HlModerator Wednesday, July 8, 2015 6:05 AM
    • Marked as answer by Codernater Thursday, July 9, 2015 6:56 PM
    Wednesday, July 8, 2015 12:12 AM

All replies

  • Hello,

    Outlook items don't provide such property. Try to pass the following string instead:

    "urn:schemas:httpmail:hasattachment" = 1

    Or you can use the Search method of the Explorer class and pass the hasattachments:true string. Unfortunately AQS is limited to the Search method of the Explorer object.

    The Search method performs a Microsoft Instant Search on the current folder displayed in the Explorer using the given Query.


    Tuesday, July 7, 2015 7:44 PM
  • Hi Eugene, 

    got a different error Condition is not valid 

    attachmentItems = mailItems.Restrict("urn:schemas:httpmail:hasattachment=1")

    I have used the 1 outside the quotation and inside didn't work. 

    Please let me know 

    thanks


    Codernater

    Tuesday, July 7, 2015 7:55 PM
  • Use the @SQL notation:

    attachmentItems = mailItems.Restrict("@SQL=""urn:schemas:httpmail:hasattachment""=1");


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

    • Proposed as answer by L.HlModerator Wednesday, July 8, 2015 6:05 AM
    • Marked as answer by Codernater Thursday, July 9, 2015 6:56 PM
    Wednesday, July 8, 2015 12:12 AM