如何选择outlook使用VBA自动发送电子邮件ITems

时间:2016-08-09 09:19:32

标签: vba email outlook

任何人都可以提供帮助,如何使用宏选择Outlook中的电子邮件项目,我需要选择使用特定日期范围的电子邮件项目 日期范围用户需要通过输入框更新,并根据日期范围自动选择这些电子邮件

我已经尝试了以下代码,它不起作用(我刚刚开始学习,任何人都可以帮助我)

Sub getEmailsSelected()

    Dim myOlSel As Outlook.Selection

    Dim myOlExp As Outlook.Explorer

    Dim gtStartDate As String

    Dim gtEndDate As String


    gtStartDate = InputBox("Type the start date (format MM/DD/YYYY)")

    gtEndDate = InputBox("Type the end date (format MM/DD/YYYY)")

    Set myOlExp = Application.ActiveExplorer

    Set myOlSel = myOlExp.Selection("[Received] >= '" & gtStartDate & "' And [Received] <= '" & gtEndDate & "'")

End Sub

2 个答案:

答案 0 :(得分:0)

为了帮助您入门:

Explorer.Selection是一个只读属性,因此您的代码无效。

要过滤/查找与您的日期范围匹配的项目,请使用Items.RestrictItems.Find

然后,您可以通过循环调用Explorer.AddToSelection来选择结果项目。

答案 1 :(得分:0)

您可以像这样组合Items.Restrict和Explorer.AddToSelection。

Option Explicit

Sub getEmailsSelected()

    Dim oFolder As Folder
    Dim oItems As Items
    Dim i As Long

    Dim gtStartDate As String
    Dim gtEndDate As String

    gtStartDate = InputBox("Type the start date (format MM/DD/YYYY)")
    gtEndDate = InputBox("Type the end date (format MM/DD/YYYY)")

    Set oFolder = ActiveExplorer.CurrentFolder
    Set oItems = oFolder.Items.Restrict("[Received] >= '" & gtStartDate & "' And [Received] <= '" & gtEndDate & "'")

    ActiveExplorer.ClearSelection

    For i = 1 To oItems.Count
        ActiveExplorer.AddToSelection oItems(i)
    Next i

End Sub