使用限制方法获取收件人,主题和日期范围

时间:2018-01-17 09:08:16

标签: vba email outlook outlook-vba outlook-filter

我试过

'only works for name, not email address.
"@SQL=""urn:schemas:httpmail:displayto"" ci_phrasematch '%John Doe%'" 

'works great, failed when use AND. today was declared as string
"@SQL=""urn:schemas:httpmail:subject"" like '%" & emailSubject & "%'"

'works great, failed when use AND. today was declared as string
"[ReceivedTime] <= today" 

如何使用Item.Restrict一起过滤收件人电子邮件地址,主题和日期范围?

2 个答案:

答案 0 :(得分:1)

以下是多个过滤器的示例

Option Explicit
Public Sub Example()
    Dim olNs As Outlook.NameSpace
    Dim TargetFolder As Outlook.MAPIFolder
    Dim Items As Outlook.Items
    Dim Item As Object
    Dim i As Long
    Dim Filter As String

    Set olNs = Application.Session
    If TargetFolder Is Nothing Then Set TargetFolder = ActiveExplorer.CurrentFolder
    Debug.Print TargetFolder.Name

    Filter = "@SQL=" & Chr(34) & "urn:schemas:httpmail:datereceived" & _
                       Chr(34) & " >= '01/10/2018' And " & _
                       Chr(34) & "urn:schemas:httpmail:datereceived" & _
                       Chr(34) & " < '01/16/2018' And " & _
                       Chr(34) & "urn:schemas:httpmail:fromname" & _
                       Chr(34) & "Like '%0m3r 0m3r%'"

    Set Items = TargetFolder.Items.Restrict(Filter)
        Items.Sort "[ReceivedTime]"

    For i = Items.Count To 1 Step -1
        DoEvents
        If TypeOf Items(i) Is mailitem Then
            Set Item = Items(i)
            Debug.Print Item.Subject
            Debug.Print Item.ReceivedTime
        End If
    Next

End Sub

<强> If TargetFolder Is Nothing Then Set TargetFolder = ActiveExplorer.CurrentFolder

  

Returns or sets a MAPIFolder object that represents the current folder displayed in the explorer

     

或使用

Dim olNs As Outlook.NameSpace
Dim TargetFolder As Outlook.MAPIFolder
Set olNs = Application.GetNamespace("MAPI")
Set TargetFolder = olNs.GetDefaultFolder(olFolderInbox)

使用主题行过滤

Filter = "@SQL=" & Chr(34) & "urn:schemas:httpmail:datereceived" & _
                   Chr(34) & " >= '01/10/2018' And " & _
                   Chr(34) & "urn:schemas:httpmail:datereceived" & _
                   Chr(34) & " < '01/17/2018' And " & _
                   Chr(34) & "urn:schemas:httpmail:fromname" & _
                   Chr(34) & "Like '%0m3r 0m3r%' And " & _
                   Chr(34) & "urn:schemas:httpmail:subject" & _
                   Chr(34) & " Like '%Bla Bla%'"

答案 1 :(得分:0)

Outlook不知道&#34;今天&#34;是。您需要提供适当格式化的日期值,例如&#34; 2018年1月17日&#34;