简体   繁体   English

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

[英]Find an email with subject starting with specific text

I am trying to find an email, by subject starting with specific text, to then download an attachment from that email.我试图通过以特定文本开头的主题查找电子邮件,然后从该电子邮件下载附件。

I am using a variable with Restrict function, however issue seems to be because of usage of wildcards.我正在使用带有 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

The first thing you should mind is that the Restrict() method does not evaluate the variable by it's name.您应该注意的第一件事是Restrict()方法不会通过变量的名称来评估变量。 You will have to concatenate the variable to the string.您必须将变量连接到字符串。

Another one is, if you look at the example from MSDN site , you will see that there is not support for wildcards, so you will have to use the SQL syntax and the searched text in the filter expression must be between quotes.另一个是,如果您查看 MSDN 站点上的示例,您会发现不支持通配符,因此您必须使用 SQL 语法,并且过滤器表达式中的搜索文本必须在引号之间。

' this namespace is for Subject
filterStr = "@SQL=""http://schemas.microsoft.com/mapi/proptag/0x0037001f"" like '%" & Findvariable & "%'"

It seems that urn:schemas:httpmail:subject also works and is easier to understand, but I can't confirm this now:似乎urn:schemas:httpmail:subject也有效并且更容易理解,但我现在无法确认:

filterStr = "@SQL=""urn:schemas:httpmail:subject"" like '%" & Findvariable & "%'"

The string comparison that DASL filters support includes equivalence, prefix, phrase, and substring matching. DASL 过滤器支持的字符串比较包括等价、前缀、短语和子字符串匹配。

For Each oOlItm In oOlInb.Items.Restrict("[Subject] = Findvariable ")对于 oOlInb.Items.Restrict("[Subject] = Findvariable ") 中的每个 oOlItm

It looks like you are searching for the exact match.看起来您正在搜索完全匹配。 But what you need is to find a substring using the following syntax:但是您需要的是使用以下语法查找子字符串:

criteria = "@SQL=" & Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " like '%question%'" 

Note that when you filter on the Subject property, prefixes such as "RE: " and "FW: " are ignored.请注意,在对 Subject 属性进行筛选时,将忽略诸如“RE:”和“FW:”之类的前缀。

See Filtering Items Using a String Comparison for more information.有关更多信息,请参阅 使用字符串比较过滤项目

PS The Restrict method is an alternative to using the Find method or FindNext method to iterate over specific items within a collection. PS Restrict 方法是使用 Find 方法或 FindNext 方法迭代集合中特定项目的替代方法。 The Find or FindNext methods are faster than filtering if there are a small number of items.如果项目数量较少,则 Find 或 FindNext 方法比过滤更快。 The Restrict method is significantly faster if there is a large number of items in the collection, especially if only a few items in a large collection are expected to be found.如果集合中有大量项目,则 Restrict 方法要快得多,尤其是当预计只能找到大型集合中的少数项目时。

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

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