查找主题以特定文本开头的电子邮件

pra*_*900 3 excel outlook vba outlook-filter

我试图通过以特定文本开头的主题查找电子邮件,然后从该电子邮件下载附件。

我正在使用带有 Restrict 函数的变量,但是问题似乎是由于使用了通配符。

Sub findemail()

cntofmkts = Range("A" & Rows.Count).End(xlUp).Row
cntofmkts = cntofmkts - 1
ftodaydate = Format(Date, "yyyy-mm-dd")

Do
    If i > cntofmkts Then Exit Do

    MarketName = Range("A" & j).Value    
    Findvariable = "XXX_" & MarketName & "_ABC_" & ftodaydate

    For Each oOlItm In oOlInb.Items.Restrict("[Subject] = *Findvariable*")
        eSender = oOlItm.SenderEmailAddress
        dtRecvd = oOlItm.ReceivedTime
        dtSent = oOlItm.CreationTime
        sSubj = oOlItm.Subject
        sMsg = oOlItm.Body

        If oOlItm.Attachments.Count <> 0 Then
            For Each oOlAtch In oOlItm.Attachments
                '~~> Download the attachment
                oOlAtch.SaveAsFile NewFileName & oOlAtch.Filename
                Exit For
            Next
        Else
            MsgBox "The First item doesn't have an attachment"
        End If

        Exit For
    Next

    i = i + 1
    j = j + 1

Loop
End sub
Run Code Online (Sandbox Code Playgroud)

Ric*_*uza 5

您应该注意的第一件事是该Restrict()方法不会通过名称来评估变量。您必须将变量连接到字符串。

另一个是,如果您查看 MSDN站点上的示例,您会发现不支持通配符,因此您必须使用 SQL 语法,并且过滤器表达式中的搜索文本必须在引号之间。

' this namespace is for Subject
filterStr = "@SQL=""http://schemas.microsoft.com/mapi/proptag/0x0037001f"" like '%" & Findvariable & "%'"
Run Code Online (Sandbox Code Playgroud)

这似乎urn:schemas:httpmail:subject也有效并且更容易理解,但我现在无法确认:

filterStr = "@SQL=""urn:schemas:httpmail:subject"" like '%" & Findvariable & "%'"
Run Code Online (Sandbox Code Playgroud)