Vba 获取由接收日期指定的从Outlook到Excel的电子邮件

Vba 获取由接收日期指定的从Outlook到Excel的电子邮件,vba,excel,outlook,outlook-filter,Vba,Excel,Outlook,Outlook Filter,我正在创建一个宏,以便在团队共享框中按主题和接收日期获取电子邮件 我使用for loop检查邮箱中的所有电子邮件,但这需要花费很长时间,因为我的声明检查了1000多封邮件 如何在指定日期前收到电子邮件?假设我需要2017年1月12日至2017年12月30日的电子邮件 关键是使用Restrict方法,但我不知道如何使用它 Sub GetFromOutlook() Dim OutlookApp As Outlook.Application Dim OutlookNamespace As Namesp

我正在创建一个宏,以便在团队共享框中按主题和接收日期获取电子邮件

我使用for loop检查邮箱中的所有电子邮件,但这需要花费很长时间,因为我的声明检查了1000多封邮件

如何在指定日期前收到电子邮件?假设我需要2017年1月12日至2017年12月30日的电子邮件

关键是使用Restrict方法,但我不知道如何使用它

Sub GetFromOutlook()
Dim OutlookApp As Outlook.Application
Dim OutlookNamespace As Namespace
Dim Folder As MAPIFolder
Dim OutlookMail As Variant
Dim i As Integer

Set OutlookApp = New Outlook.Application
Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")

Dim olShareName As Outlook.Recipient
Set olShareName = OutlookNamespace.CreateRecipient("sharemailbox@example.ca")
Set Folder = OutlookNamespace.GetSharedDefaultFolder(olShareName, olFolderInbox).Folders("sharebox subfolder").Folders("sharebox subfolder2")

i = 1

For Each OutlookMail In Folder.Items

    If ((Range("From_Date").Value <= OutlookMail.ReceivedTime) And _
      (OutlookMail.ReceivedTime <= Range("To_Date").Value)) And _
      OutlookMail.Sender = "sender@example.com" Then

        Range("eMail_subject").Offset(i, 0).Value = OutlookMail.Subject
        Range("eMail_date").Offset(i, 0).Value = OutlookMail.ReceivedTime

        i = i + 1

    End If

Next OutlookMail

Set Folder = Nothing
Set OutlookNamespace = Nothing
Set OutlookApp = Nothing

End Sub
子GetFromOutlook()
Dim OutlookApp作为Outlook.Application
Dim OutlookNamespace作为命名空间
将文件夹设置为MAPIFolder
Dim OutlookMail作为变体
作为整数的Dim i
Set-OutlookApp=新建Outlook.Application
设置OutlookNamespace=OutlookApp.GetNamespace(“MAPI”)
将ShareName设置为Outlook.Recipient
设置olShareName=OutlookNamespace.CreateRecipient(“sharemailbox@example.ca")
Set Folder=OutlookNamespace.GetSharedDefaultFolder(olShareName,olFolderInbox).Folders(“sharebox子文件夹”).Folders(“sharebox子文件夹2”)
i=1
对于文件夹中的每个OutlookMail.Items

If((Range(“From_Date”).Value您可能可以使用
GetTable
,而不是必须逐个处理每个电子邮件(或项目)的循环。
GetTable
将允许您对文件夹的内容应用一个过滤器,该过滤器应该运行得更快

有关更多详细信息和示例,您可以查看

对于您尝试应用的特定筛选器,我将尝试:

"([ReceivedTime]>=12/1/17) AND ([ReceivedTime]<=12/30/17)"

”([ReceivedTime]>=12/1/17)和([ReceivedTime]您可以创建受日期限制的项目集合,如下所示

Option Explicit

Private Sub EmailInTimePeriod()

    Dim oOlInb As Folder
    Dim oOlItm As Object

    Dim oOlResults As Object
    Dim i As Long

    Dim sFilterLower As String
    Dim sFilterUpper As String
    Dim sFilter As String

    Dim dStart As Date
    Dim dEnd As Date

    Set oOlInb = Session.GetDefaultFolder(olFolderInbox)

    ' https://msdn.microsoft.com/en-us/library/office/ff869597.aspx

    ' 12/1/2017 to 12/30/2017
    'dStart = "2017/12/01"
    'dEnd = "2017/12/30"

    ' 1/12/2018 to 1/15/2018
    dStart = "2018/01/12"
    dEnd = "2018/01/16"

    ' Lower Bound of the range
    sFilterLower = "[ReceivedTime]>'" & Format(dStart, "DDDDD HH:NN") & "'"
    Debug.Print vbCr & "sFilterLower: " & sFilterLower


    ' *** temporary demo lines
    ' Restrict the items in the folder
    Set oOlResults = oOlInb.Items.Restrict(sFilterLower)
    Debug.Print oOlResults.count & " items."

    If oOlResults.count > 0 Then
        For i = 1 To oOlResults.count
            Set oOlItm = oOlResults(i)
            Debug.Print oOlItm.ReceivedTime & " - " & oOlItm.subject
        Next i
    End If
    ' *** temporary demo lines


    ' Upper Bound of the range
    sFilterUpper = "[ReceivedTime]<'" & Format(dEnd, "DDDDD HH:NN") & "'"
    Debug.Print vbCr & "sFilterUpper: " & sFilterUpper


    ' *** temporary demo lines
    ' Restrict the Lower Bound result
    Set oOlResults = oOlResults.Restrict(sFilterUpper)
    Debug.Print oOlResults.count & " items."

    If oOlResults.count > 0 Then
        For i = 1 To oOlResults.count
            Set oOlItm = oOlResults(i)
            Debug.Print oOlItm.ReceivedTime & " - " & oOlItm.subject
        Next i
    End If
    ' *** temporary demo lines


    ' combine the filters
    sFilter = sFilterLower & " AND " & sFilterUpper
    Debug.Print vbCr & "sFilter: " & sFilter

    Set oOlResults = oOlInb.Items.Restrict(sFilter)
    Debug.Print oOlResults.count & " items."

    If oOlResults.count > 0 Then
        For i = 1 To oOlResults.count
            Set oOlItm = oOlResults(i)
            Debug.Print oOlItm.ReceivedTime & " - " & oOlItm.subject
        Next i
    End If


ExitRoutine:
    Set oOlInb = Nothing
    Set oOlResults = Nothing
    Set oOlItm = Nothing
    Debug.Print "Done."

End Sub
选项显式
私有子电子邮件初始化周期()
Dim oOlInb As文件夹
作为对象的Dim oOlItm
将结果设置为对象
我想我会坚持多久
作为字符串的Dim sFilterLower
将sFilterUpper设置为字符串
作为字符串的Dim sFilter
开始日期
暗淡无光
设置oOlInb=Session.GetDefaultFolder(olFolderInbox)
' https://msdn.microsoft.com/en-us/library/office/ff869597.aspx
'2017年12月1日至2017年12月30日
'dStart=“2017/12/01”
'dEnd=“2017/12/30”
'2018年1月12日至2018年1月15日
dStart=“2018/01/12”
dEnd=“2018/01/16”
“范围下限
sFilterLower=“[ReceivedTime]>”&格式(dStart,“DDDDD HH:NN”)&“
调试。打印vbCr和“sFilterLower:&sFilterLower”
'***临时演示行
'限制文件夹中的项目
设置oOlResults=oOlInb.Items.Restrict(sFilterLower)
Debug.Print oOlResults.count&“项”
如果oOlResults.count>0,则
对于i=1到o.results.count
设置oOlItm=oOlResults(i)
Debug.Print oOlItm.ReceivedTime&“-”&oOlItm.subject
接下来我
如果结束
'***临时演示行
“范围的上限

sFilterUpper=“[ReceivedTime]我按照您的建议使用了GetTable,但如何定义筛选器?我的语句是Filter=“[LastModificationTime]>'1/12/2018',但我希望按接收日期指定。例如)我需要2018年1月12日至2018年1月15日的电子邮件。有人能帮我吗?我编辑了我的答案以解决你问题的这一部分。相关:谢谢你的帮助。这对我来说很有效,但现在的问题是,如果我想收到2018年1月12日至2018年1月16日的电子邮件,那么在15封选择和存储对象中只有10封。在5封电子邮件中,有相同的接收日期,但不是有人能帮忙吗?用您当前的代码为问题创建另一个问题。回滚此编辑并创建一个新问题,这样您就不会使已收到的答案无效。@Jason我已经为您完成了回滚。(我正要问职业训练局你的新问题,因为它与这个问题相同,但后来注意到有评论说你把这个问题编辑成了不同的内容。)@YowE3K谢谢,我是新来的,我不知道。
Option Explicit

Private Sub EmailInTimePeriod()

    Dim oOlInb As Folder
    Dim oOlItm As Object

    Dim oOlResults As Object
    Dim i As Long

    Dim sFilterLower As String
    Dim sFilterUpper As String
    Dim sFilter As String

    Dim dStart As Date
    Dim dEnd As Date

    Set oOlInb = Session.GetDefaultFolder(olFolderInbox)

    ' https://msdn.microsoft.com/en-us/library/office/ff869597.aspx

    ' 12/1/2017 to 12/30/2017
    'dStart = "2017/12/01"
    'dEnd = "2017/12/30"

    ' 1/12/2018 to 1/15/2018
    dStart = "2018/01/12"
    dEnd = "2018/01/16"

    ' Lower Bound of the range
    sFilterLower = "[ReceivedTime]>'" & Format(dStart, "DDDDD HH:NN") & "'"
    Debug.Print vbCr & "sFilterLower: " & sFilterLower


    ' *** temporary demo lines
    ' Restrict the items in the folder
    Set oOlResults = oOlInb.Items.Restrict(sFilterLower)
    Debug.Print oOlResults.count & " items."

    If oOlResults.count > 0 Then
        For i = 1 To oOlResults.count
            Set oOlItm = oOlResults(i)
            Debug.Print oOlItm.ReceivedTime & " - " & oOlItm.subject
        Next i
    End If
    ' *** temporary demo lines


    ' Upper Bound of the range
    sFilterUpper = "[ReceivedTime]<'" & Format(dEnd, "DDDDD HH:NN") & "'"
    Debug.Print vbCr & "sFilterUpper: " & sFilterUpper


    ' *** temporary demo lines
    ' Restrict the Lower Bound result
    Set oOlResults = oOlResults.Restrict(sFilterUpper)
    Debug.Print oOlResults.count & " items."

    If oOlResults.count > 0 Then
        For i = 1 To oOlResults.count
            Set oOlItm = oOlResults(i)
            Debug.Print oOlItm.ReceivedTime & " - " & oOlItm.subject
        Next i
    End If
    ' *** temporary demo lines


    ' combine the filters
    sFilter = sFilterLower & " AND " & sFilterUpper
    Debug.Print vbCr & "sFilter: " & sFilter

    Set oOlResults = oOlInb.Items.Restrict(sFilter)
    Debug.Print oOlResults.count & " items."

    If oOlResults.count > 0 Then
        For i = 1 To oOlResults.count
            Set oOlItm = oOlResults(i)
            Debug.Print oOlItm.ReceivedTime & " - " & oOlItm.subject
        Next i
    End If


ExitRoutine:
    Set oOlInb = Nothing
    Set oOlResults = Nothing
    Set oOlItm = Nothing
    Debug.Print "Done."

End Sub