【问题标题】:Iterating quickly through Outlook appointment items快速遍历 Outlook 约会项目
【发布时间】:2020-06-21 07:23:40
【问题描述】:

我编写了一个宏,它遍历用户日历并对满足特定条件的条目进行修改。

问题是当日历很大时,这需要很长时间才能完成。我似乎无法过滤约会,因为oAppointmentItems 似乎在创建条目时存储了它们 - 这不一定与它们开始时的顺序相同。

我使用的代码是这样的:

Dim oOL As New Outlook.Application
Dim oNS As Outlook.NameSpace
Dim oAppointments As Object
Dim oAppointmentItem As Outlook.AppointmentItem

Set oNS = oOL.GetNamespace("MAPI")
Set oAppointments = oNS.GetDefaultFolder(olFolderCalendar)

For Each oAppointmentItem In oAppointments.Items

    DoEvents
    ' Something here
Next

Set oAppointmentItem = Nothing
Set oAppointments = Nothing
Set oNS = Nothing
Set oOL = Nothing

没有删除DoEvents(这只意味着 Outlook 似乎锁定了用户)有没有什么办法可以通过应用某种过滤器来加快速度?例如,未来开始的约会。

【问题讨论】:

    标签: vba outlook calendar iteration


    【解决方案1】:

    您可以使用限制进行过滤。请注意,日期的格式为月、日、年,并且它们被过滤为字符串,即使存储为日期:

    Set olApp = CreateObject("Outlook.Application")
    Set olNS = olApp.GetNamespace("MAPI")
    
    Set olRecItems = olNS.GetDefaultFolder(olFolderTasks)
    strFilter = "[DueDate] > '1/15/2009'"
    Set olFilterRecItems = olRecItems.Items.Restrict(strFilter)
    
    
    For i = 1 To olFilterRecItems.Count
      <...>
    

    更多信息:http://msdn.microsoft.com/en-us/library/bb220369.aspx

    【讨论】:

    • 这正是我今天想要的!这为我省去了很多麻烦。我注意到的一件事是,我无法使用 = 使用日期过滤器,而且很难获得准确的日期(似乎取决于它们是 Outlook 中的日期还是日期/时间)。 Like > Date - 1 天和 Date And Date
    • 在 VBA 中使用早期绑定,olRecItems 应该如何变暗? (外表。???)。谢谢..
    • @iDevlop 作为 Outlook.MAPIFolder AFAIK。
    • 对于未来的读者,您可以通过在分配变量后调用TypeName 函数来获取早期绑定的对象类型:Dim olRecItemsSet olRecItems = CreateObject("Outlook.Application").GetNamespace("MAPI").GetDefaultFolder(olFolderTasks)Debug.Print TypeName(olRecItems) 这打印@ 987654327@.
    【解决方案2】:

    嘿,无法执行任务,但这似乎适用于约会 full explaination

    Dim myStart As Date
    Dim myEnd As Date
    
    myStart = Date
    myEnd = DateAdd("d", 30, myStart)
    
    Debug.Print "Start:", myStart
    Debug.Print "End:", myEnd
    
    'Construct filter for the next 30-day date range
    strRestriction = "[Start] >= '" & _
    Format$(myStart, "mm/dd/yyyy hh:mm AMPM") _
    & "' AND [End] <= '" & _
    Format$(myEnd, "mm/dd/yyyy hh:mm AMPM") & "'"
    'Check the restriction string
    Debug.Print strRestriction
    
    Const olFolderCalendar = 9
    Set olApp = CreateObject("Outlook.Application")
    Set olNS = olApp.GetNamespace("MAPI")
    
    Set oCalendar = olNS.GetDefaultFolder(olFolderTasks)
    
    Set oItems = oCalendar.items
    oItems.IncludeRecurrences = True
    ' oItems.Sort "[Start]" ' commented out  worked for me.. 
    'Restrict the Items collection for the 30-day date range
    Set oItemsInDateRange = oItems.Restrict(strRestriction)
    Debug.Print oItemsInDateRange.Count
    

    【讨论】:

      猜你喜欢
      • 2014-09-16
      • 1970-01-01
      • 1970-01-01
      • 2017-05-08
      • 1970-01-01
      • 2011-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多