Outlook日历(VBA)中的Items.Restrict约会
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Outlook日历(VBA)中的Items.Restrict约会相关的知识,希望对你有一定的参考价值。
我试图让日历中的所有约会在本月5日到下个月4日之间发生(包括那些日子发生的约会)。
这是代码:
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()函数只是获取当前月份的第一天和最后一天。
我在2018年1月4日举办了两场比赛,一场是持续一整天的经常性赛事,另一场是一整天持续的独立赛事。
不幸的是,只有经常发生的事件才能通过。如果我让他们两个都经常性,那么他们都被抓到了monthPats,这就是我想要的。
任何人都可以解释并提供这个问题的解决方案,因为它没有意义吗?
答案
事实证明,限制outlook中的项目可能是一场噩梦,我确保将IncludeRecurrences属性设置为True并按开始日期排序,这似乎可以解决问题。
此外,我让限制字符串同时执行两个作业,似乎更健壮一些:
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
以上是关于Outlook日历(VBA)中的Items.Restrict约会的主要内容,如果未能解决你的问题,请参考以下文章
是否有 VBA 方法可以在 Outlook 中创建新日历(不是约会)
如何使用 VBA 在 Outlook 365 上启动时显示所有日历?