查找主题以特定文本开头的电子邮件 [英] 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.
我正在使用带有Restrict函数的变量,但是问题似乎是由于使用了通配符.
I am using a variable with Restrict function, however issue seems to be because of usage of wildcards.
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
推荐答案
您应该注意的第一件事是,Restrict()
方法不会使用其名称来评估变量.您必须将变量连接到字符串.
The first thing you should mind is that the Restrict()
method does not evaluate the variable by it's name. You will have to concatenate the variable to the string.
另一种方法是,如果您查看MSDN中的示例站点,您将看到不支持通配符,因此您将必须使用SQL语法,并且过滤器表达式中的搜索文本必须在引号之间.
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.
' this namespace is for Subject
filterStr = "@SQL=""http://schemas.microsoft.com/mapi/proptag/0x0037001f"" like '%" & Findvariable & "%'"
urn:schemas:httpmail:subject
似乎也可以工作并且更易于理解,但是我现在无法确认:
It seems that urn:schemas:httpmail:subject
also works and is easier to understand, but I can't confirm this now:
filterStr = "@SQL=""urn:schemas:httpmail:subject"" like '%" & Findvariable & "%'"
这篇关于查找主题以特定文本开头的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!