Items.Outlook Outlook(VBA)中限制约会 [英] Items.Restrict appointments in Outlook Calendar (VBA)
问题描述
我正在尝试获取本月5日至下个月4日之间的日历中的所有约会(包括当日发生的约会)。
I'm trying to get all appointments in a calendar occurring between the 5th of this month and the 4th of next month (including appointments that happen on those days).
这是代码:
Private Sub Application_Startup()
Dim oOL As New Outlook.Application
Dim oNS As Outlook.NameSpace
Dim oAppointments As Object
Dim monthlyPats As Object
Dim oAppointmentItem As Outlook.AppointmentItem
'Set up date filter
Dim sMth As Date
Dim eMth As Date
sMth = dhFirstDayInMonth() + 3 '4th of this month
eMth = dhLastDayInMonth() + 4 '4th of next month
Dim eDate As String
eDate = "[End] < '" & eMth & "'"
Dim sDate As String
sDate = "[Start] > '" & sMth & "'"
'Restrict tasks based on date filters
Set oNS = oOL.GetNamespace("MAPI")
Set oAppointments = Session.GetDefaultFolder(olFolderCalendar).Folders("Portfolio analysis scheduler").Items.Restrict(eDate)
Set monthlyPats = oAppointments.Restrict(sDate)
End Sub
dhFirstDayInMonth()和dhLastDayInMonth()函数仅获取当前月份的第一天和最后一天。
The dhFirstDayInMonth() and dhLastDayInMonth() functions simply get the first and last day of the current month.
我在2018年1月4日有两个事件,一个是持续一整天的周期性事件,另一个是一个持续一整天的独立事件。
I have two events on the 4th January 2018, one is a recurrent event that lasts all day and the other is just a standalone event that lasts all day.
不幸的是,只有重复发生的事件才能通过。如果我让它们都反复出现,那么它们都会被捕获在我想要的monthlyPats中。
Unfortunately, only the recurrent event pulls through. If I make both of them recurrent then they both get captured in monthlyPats which is what I want.
有人可以解释一下并提供解决方案,因为它使
Can anyone please explain and offer a solution to this problem as it makes no sense?
推荐答案
原来,限制Outlook中的项目可能是一场噩梦,我确保将IncludeRecurrences属性设置为True,然后
Turns out that restricting items in outlook can be a nightmare, I made sure the IncludeRecurrences property was set to True and sorted by start date, this seemed to do the trick.
此外,我使限制字符串同时执行两项工作,似乎更可靠:
Also, I made the restriction string do both jobs at once, seems to be a bit more robust:
Private Sub Application_Startup()
Dim oOL As New Outlook.Application
Dim oNS As Outlook.NameSpace
Dim allAppointments As Object
Dim oAppointments As Object
Dim monthlyPats As Object
Dim oAppointmentItem As Outlook.AppointmentItem
'Set up date filter
Dim sMth As Date
Dim eMth As Date
sMth = dhFirstDayInMonth() + 3 '4th of this month
eMth = dhLastDayInMonth() + 5 '5th of next month
Dim rstDate As String
rstDate = "[Start] > '" & sMth & "'" & " AND " & "[End] < '" & eMth & "'"
'Restrict tasks based on date filters
Set oNS = oOL.GetNamespace("MAPI")
Set allAppointments = Session.GetDefaultFolder(olFolderCalendar).Folders("Portfolio analysis scheduler").Items
allAppointments.IncludeRecurrences = True
allAppointments.Sort "[Start]"
Set monthlyPats = allAppointments.Restrict(rstDate)
monthlyPats.IncludeRecurrences = True
End Sub
这篇关于Items.Outlook Outlook(VBA)中限制约会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!