使用 Outlook VBA 中的 .Restrict 方法筛选单个收件人电子邮件地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16286694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using the .Restrict method in Outlook VBA to filter on single recipient email address
提问by Jaspos
I have code in Access that gets all emails in the user's Inbox that are sent by an individual email address. This code (simplified, below) works fine:
我在 Access 中有代码,可以获取用户收件箱中由单个电子邮件地址发送的所有电子邮件。此代码(简化,如下)工作正常:
Dim outItems as Outlook.Items
Dim strEMAddress as string
Dim outFolder as Outlook.MAPIFolder
Set outFolder = outNS.GetDefaultFolder(olFolderInbox)
Set outItems = outFolder.Items
str="[email protected]"
Set outItems = outItems.Restrict("[SenderEmailAddress] = " & "'" & strEMAddress & "'")
I am looking for something that will do likewise on the SentMails folder, restricting the items to those sent to a specific email address.
我正在寻找同样适用于 SentMails 文件夹的内容,将项目限制为发送到特定电子邮件地址的项目。
I know this is complicated by the fact that .Recipients is a collection (as items can/do have more than one recipient). I am hoping there is a way to return a list of items that contain the email address I am looking for in any of the sent fields (To/CC/bcc - but happy with just To if this is easier).
我知道这很复杂,因为 .Recipients 是一个集合(因为项目可以/确实有多个收件人)。我希望有一种方法可以返回包含我在任何已发送字段中查找的电子邮件地址的项目列表(收件人/抄送/密件抄送 - 但如果这更容易的话,只对收件人感到满意)。
I have searched online and found .To is no good (is not the email address) and I can't get pseudo code such as this work:
我在网上搜索过,发现 .To 不好(不是电子邮件地址),而且我无法获得这样的伪代码:
Set outItems = outItems.Restrict("[Recipients] = " & "'" & strEMAddress & "'")
回答by Dmitry Streblechenko
If using Redemptionis an option, you can use RDOFolder.Items.Restrict- unlike Outlook Object Model, it does expand To/CC/BCC queries into recipient sub restrictions on PR_DISPLAY_NAME
and PR_EMAIL_ADDRESS
properties on each recipient (RES_SUBRESTRICTION / PR_MESSAGE_RECIPIENTS / RES_OR / PR_DISPLAY_NAME | PR_EMAIL_ADDRESS).
如果可以选择使用Redemption,则可以使用 RDOFolder。Items.Restrict- 与 Outlook 对象模型不同,它确实将 To/CC/BCC 查询扩展为每个收件人的收件人子限制PR_DISPLAY_NAME
和PR_EMAIL_ADDRESS
属性 (RES_SUBRESTRICTION / PR_MESSAGE_RECIPIENTS / RES_OR / PR_DISPLAY_NAME | PR_EMAIL_ADDRESS)。
set Session = CreateObject("Redemption.RDOSession")
Session.MAPIOBJECT = Application.Session.MAPIOBJECT
set Folder = Session.GetFolderFromID(Application.ActiveExplorer.CurrentFolder.EntryID)
set restrItems = Folder.Items.Restrict(" TO = '[email protected]' ")
You can also specify Recipients
property in the SQL query - it will be matched against recipients of all types (to/cc/bb):
您还可以Recipients
在 SQL 查询中指定属性 - 它将与所有类型的收件人(to/cc/bb)匹配:
set restrItems = Folder.Items.Restrict(" Recipients = '[email protected]' ")
回答by S.aad
You can use the DASL query as the filter string in your items.restrictmethod.
您可以在items.restrict方法中使用 DASL 查询作为过滤器字符串。
For example to find all mails i sent to Ali Raza i use the following
例如,要查找我发送给 Ali Raza 的所有邮件,我使用以下命令
str_fltr = "@SQL=""urn:schemas:httpmail:displayto"" ci_phrasematch '%Ali Raza%'"
The good thing about the above DASL query is that it returns matches with multiple recepients whether if you use the jet syntax for searching resultx will only contain items with one recipient. Jet syntax is the one that you are currently using. You should use the [To]property rather than [Recipients]
上述 DASL 查询的好处是它返回与多个收件人的匹配项,无论您是否使用 jet 语法搜索 resultx 将只包含一个收件人的项目。Jet 语法是您当前使用的语法。您应该使用[To]属性而不是[Recipients]
Here https://msdn.microsoft.com/en-us/library/cc513841%28v=office.12%29.aspx#SearchingOutlookData_Overviewis good place where you can learn almost everything about searching in outlook.
在这里https://msdn.microsoft.com/en-us/library/cc513841%28v=office.12%29.aspx#SearchingOutlookData_Overview是您可以了解有关 Outlook 搜索的几乎所有内容的好地方。
Here http://www.msoffice.us/Outlook/PDF/%28Outlook%202010%29%20Common%20DASL%20Property%20Tags.pdfis a list of common DASL tags which will come in handy if you get a grip on DASL syntax.
这里http://www.msoffice.us/Outlook/PDF/%28Outlook%202010%29%20Common%20DASL%20Property%20Tags.pdf是一个常见的 DASL 标签列表,如果你掌握了 DASL 就会派上用场句法。
回答by 0m3r
For multiple [TO/CC/BCC]
filter example would be...
对于多个[TO/CC/BCC]
过滤器示例将是...
Option Explicit
Public Sub Example()
Dim olNs As Outlook.NameSpace
Dim Folder As Outlook.MAPIFolder
Dim Items As Outlook.Items
Dim Filter As String
Dim Msg As String
Dim i As Long
Set olNs = Application.GetNamespace("MAPI")
Set Folder = olNs.GetDefaultFolder(olFolderSentMail)
Filter = "@SQL=" & "urn:schemas:httpmail:displayto" & _
" Like '%John Doe%' Or " & _
"urn:schemas:httpmail:displaycc" & _
" Like '%John Doe%' Or " & _
"urn:schemas:httpmail:displaybcc" & _
" Like '%John Doe%'"
Set Items = Folder.Items.Restrict(Filter)
Msg = Items.Count & " Items in " & Folder.Name & " Folder"
If MsgBox(Msg, vbYesNo) = vbYes Then
For i = Items.Count To 1 Step -1
Debug.Print Items(i) 'Immediate Window
Next
End If
End Sub
now remember if the display name is [email protected]
then filter should be %[email protected]%
else use %John Doe%
现在记住如果显示名称是[email protected]
那么过滤器应该是%[email protected]%
其他使用%John Doe%