Items.Restrict appointments in Outlook Calendar (VBA)

心不动则不痛 提交于 2020-01-06 08:31:54

问题


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).

Here is the code:

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

The dhFirstDayInMonth() and dhLastDayInMonth() functions simply get the first and last day of the current month.

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.

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?


回答1:


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


来源:https://stackoverflow.com/questions/47924078/items-restrict-appointments-in-outlook-calendar-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!