将搜索限制在约会的最后一周

时间:2016-11-23 22:24:46

标签: vba outlook

我试图在Outlook中使用VBA获取最后一周的约会。

我使用的是.Restrict方法,但有些东西让我的字符串更长时间地被抓住了。

我首先声明我的时间段的格式化日期:

myStart = Format(DateAdd("d", -7, Now()), "ddddd h:nn AMPM")
myEnd = Format(Now(), "ddddd h:nn AMPM")  

我构建一个字符串来保持我的限制标准。

strRestriction = "[Start] <= '" & myEnd _
& "' AND [End] >= '" & myStart & "'"

最后,我打电话限制我的预约项目:

Set oRestrItems = oItems.Restrict(strRestriction)

有关更多内容,请参阅以下内容:

For Each oApptItem In oRestrItems 'oItems will grab everything, but that's hardly perfect.
    If oApptItem.Sensitivity <> olPrivate Then
        MsgBox (oApptItem.Subject)
        MsgBox (oApptItem.Start)
        MsgBox (oApptItem.End)
    End If
Next

1 个答案:

答案 0 :(得分:1)

我猜你错过了两个陈述。

oItems.IncludeRecurrences = True
oItems.Sort "[Start]"

如果是这种情况,您可以询问有关Restrict为何需要这些附加声明的另一个问题。有人可能有答案。

最小,完整且可验证的示例。尝试注释掉其中一个或两个语句。您应该看到这些项目不一样。

Option Explicit

Sub LastWeekAppts()

Dim objFolder As Folder

Dim oItems As items
Dim oRestrItems As items

Dim strRestriction As String
Dim myStart As Date
Dim myEnd As Date

Dim temp As Object

Set objFolder = Session.GetDefaultFolder(olFolderCalendar)
Set oItems = objFolder.items

' *****************************************
' Odd results without these two statements
oItems.IncludeRecurrences = True
oItems.Sort "[Start]"
' *****************************************

myEnd = Date
myStart = DateAdd("d", -7, Date)

Debug.Print " myStart: " & myStart
Debug.Print " myEnd  : " & myEnd

strRestriction = "[Start] <= '" & myEnd _
                      & "' AND [End] >= '" & myStart & "'"
Debug.Print strRestriction

Set oRestrItems = oItems.Restrict(strRestriction)

For Each temp In oRestrItems
    Debug.Print temp.start & " - " & temp.Subject
Next temp

End Sub