【问题标题】:Find all appointments in specified period with Restrict使用限制查找指定时间段内的所有约会
【发布时间】:2018-06-04 01:53:26
【问题描述】:

我正在尝试获取日历中发生在本月 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 日有两个活动,一个是持续一整天的周期性活动,另一个是持续一整天的独立活动。

只有经常性事件才能通过。如果我让它们都重复出现,那么它们都会在monthlyPats 中被捕获,这就是我想要的。

【问题讨论】:

    标签: vba filter outlook calendar appointment


    【解决方案1】:

    事实证明,在 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-26
      • 2021-12-31
      • 2022-06-11
      • 1970-01-01
      • 2016-05-16
      • 2016-07-08
      • 2016-04-10
      相关资源
      最近更新 更多