2016-11-23 11 views
0

我试图抓住最后一周在Outlook中使用VBA的约会。限制搜索约会的最后一周

我正在使用.Restrict方法,但是有些东西让我的字符串在3年前被抢回。

我开始通过声明格式的日期对我的时间支架:

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 
+0

如果您更容易为响应者提供帮助,获得响应的机会更大。 http://stackoverflow.com/help/mcve – niton

回答

0

我能猜到你缺少两个陈述。

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