简体   繁体   中英

Excel vba attachment search

I am working on macro which would find string within outlook mails attachments. I have working module searching through subject, body and attachments names on given mailbox and folder.

Problem is that my code don't want to emulate outlook search within attachment function.

Code searches for a word 'office' within mail subject field and displays found mails:

Sub t22()

Dim myolApp As Outlook.Application
Dim objNS As Outlook.Namespace
Dim objFolder As Outlook.MAPIFolder
Dim ProcessName As String
Dim EmailName As String

Set myolApp = CreateObject("Outlook.Application")
Set objNS = myolApp.GetNamespace("MAPI")
Set objFolder = objNS.Folders("my@mailbox.com")
Set TargetInbox = objFolder.Folders("Inbox")

Dim oItms As Outlook.Items
Dim oItm As Outlook.MailItem
Set oItms = TargetInbox.Items
Dim sFilter As String
Dim EmailTime As String

sFilter = "@SQL=""http://schemas.microsoft.com/mapi/proptag/0x0037001f"" = 'office'"
Set oItm = oItms.Find(sFilter)
'If Not IsEmpty(oltm) Then
    oItm.Display
    Debug.Print oItm.Body
'End If
End Sub

As far as I understand 'sFilter' should be referring to target search fields but when I use its value for attachments (0x0EA5001E) it fails.

I was also trying AdvancedSearch method but with same result - working for everything other than attachment.

The Outlook object model doesn't provide anything for searching a string in attachments. You need to find all items that have files attached to them and then iterate over all of them. While iterating you can open the attached file and search for a string inside. You can use the following search criteria to find all items that have attachments:

 query ="@SQL=" & chr(34) & "urn:schemas:httpmail:hasattachment" & chr(34) & "=1"

You may also find the following articles helpful:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM