简体   繁体   English

Excel VBA附件搜索

[英]Excel vba attachment search

I am working on macro which would find string within outlook mails attachments. 我正在研究可在Outlook邮件附件中找到字符串的宏。 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. 问题是我的代码不想在附件函数中模拟Outlook搜索。

Code searches for a word 'office' within mail subject field and displays found mails: 代码在邮件主题字段中搜索单词“ office”,并显示找到的邮件:

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. 据我了解,“ sFilter”应指向目标搜索字段,但是当我将其值用于附件(0x0EA5001E)时,它将失败。

I was also trying AdvancedSearch method but with same result - working for everything other than attachment. 我也在尝试AdvancedSearch方法,但结果相同-除附件外,其他都可以使用。

The Outlook object model doesn't provide anything for searching a string in attachments. Outlook对象模型不提供任何用于搜索附件中的字符串的功能。 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: 您可能还会发现以下文章有所帮助:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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