VBA筛选今天的全天重复Outlook约会

huangapple go评论87阅读模式
英文:

VBA Filter All-Day Outlook Reoccurring Appointments for Today

问题

如何使用VBA的restrict函数来筛选今天的全天重复的Outlook日历约会?我有一个每两周重复一次的全天约会。

我的尝试:

  1. Dim olApp As Outlook.Application
  2. Set olApp = Outlook.Application
  3. Set calendarItems = olApp.Session.GetDefaultFolder(olFolderCalendar).Items
  4. '文档说首先对集合进行排序,然后设置包括重复项
  5. 'https://learn.microsoft.com/en-us/office/vba/api/outlook.items.includerecurrences
  6. calendarItems.Sort "[Start]"
  7. calendarItems.IncludeRecurrences = True
  8. '用于restrict的筛选条件:[Start] < "明天" 并且 [Start] >= "今天"
  9. criteria = "[Start] < " & Chr(34) & VBA.Format(Now + 1, "Short Date") & Chr(34) & " and [Start] >= " & Chr(34) & VBA.Format(Now, "Short Date") & Chr(34)
  10. Set todayAppointments = calendarItems.Restrict(criteria)
  11. '用于调试的打印信息
  12. For Each Item In todayAppointments
  13. Debug.Print (Item.Subject & " " & Item.ConversationTopic & " " & Item.Organizer & " " & Item.Start & " " & Item.End)
  14. Next

这会显示出今天发生的普通约会,但不包括重复的约会。

如果我扩展筛选条件以包括昨天一天,即VBA.Format(Now - 1, "Short Date"),那么它会包括今天的重复约会。但是,那样也会包括前一天的其他约会,这不是我想要的。

英文:

How can I use the VBA restrict function to filter for all day reoccurring Outlook calendar appointments that are today? I have a reoccurring appointment every other week and it's set as an all-day appointment.

My attempt:

  1. Dim olApp As Outlook.Application
  2. Set olApp = Outlook.Application
  3. Set calendarItems = olApp.Session.GetDefaultFolder(olFolderCalendar).Items
  4. &#39;Documentation says sort the collection first, then set include recurrences
  5. &#39;https://learn.microsoft.com/en-us/office/vba/api/outlook.items.includerecurrences
  6. calendarItems.Sort &quot;[Start]&quot;
  7. calendarItems.IncludeRecurrences = True
  8. &#39;filter for restrict: [Start] &lt; &quot;&lt;tomorrow&gt;&quot; and [Start] &gt;= &quot;&lt;today&gt;&quot;
  9. criteria = &quot;[Start] &lt; &quot; &amp; Chr(34) &amp; VBA.Format(Now + 1, &quot;Short Date&quot;) &amp; Chr(34) &amp; &quot; and [Start] &gt;= &quot; &amp; Chr(34) &amp; VBA.Format(Now, &quot;Short Date&quot;) &amp; Chr(34)
  10. Set todayAppointments = calendarItems.Restrict(criteria)
  11. &#39;Print info for debugging
  12. For Each Item In todayAppointments
  13. Debug.Print (Item.Subject &amp; &quot; &quot; &amp; Item.ConversationTopic &amp; &quot; &quot; &amp; Item.Organizer &amp; &quot; &quot; &amp; Item.Start &amp; &quot; &quot; &amp; Item.End)
  14. Next

This will bring up the regular appointments that occur today, but not the reoccurring appointment.

I can get the reoccurring appointment if I expand my filter to be one day before today, i.e. VBA.Format(Now - 1, &quot;Short Date&quot;), then it brings in my reoccurring appointment for today. However, then I also get other appointments for the previous day which is not what I wanted.

答案1

得分: 1

添加一个时间组件。

  1. Option Explicit ' 将此视为强制性的
  2. ' 工具 | 选项 | 编辑器选项卡
  3. ' 要求变量声明
  4. ' 如果非常需要,请声明为Variant
  5. Private Sub ApptToday()
  6. Dim calendarItems As Items
  7. Dim todayAppointments As Items
  8. Dim item As Object
  9. Set calendarItems = Session.GetDefaultFolder(olFolderCalendar).Items
  10. ' 文档建议首先对集合进行排序,然后设置包括重复项
  11. ' https://learn.microsoft.com/en-us/office/vba/api/outlook.items.includerecurrences
  12. calendarItems.Sort "[Start]"
  13. calendarItems.IncludeRecurrences = True
  14. Dim criteria1 As String
  15. criteria1 = "[Start] < " & Chr(34) & VBA.Format(Now + 1, "Short Date") & Chr(34) & " AND [Start] >= " & Chr(34) & VBA.Format(Now, "Short Date") & Chr(34)
  16. Debug.Print
  17. Debug.Print criteria1
  18. Set todayAppointments = calendarItems.Restrict(criteria1)
  19. ' 用于调试的打印信息
  20. For Each item In todayAppointments
  21. If item.Class = olAppointment Then
  22. Debug.Print (item.subject & " " & item.ConversationTopic & " " & item.Organizer & " " & item.Start & " " & item.End)
  23. End If
  24. Next
  25. Dim criteria2 As String
  26. criteria2 = "[Start] < " & Chr(34) & VBA.Format(Now + 1, "Short Date") & " 00:00" & Chr(34) & " AND [Start] >= " & Chr(34) & VBA.Format(Now, "Short Date") & " 00:00" & Chr(34)
  27. Debug.Print
  28. Debug.Print criteria2
  29. Set todayAppointments = calendarItems.Restrict(criteria2)
  30. ' 用于调试的打印信息
  31. For Each item In todayAppointments
  32. If item.Class = olAppointment Then
  33. Debug.Print (item.subject & " " & item.ConversationTopic & " " & item.Organizer & " " & item.Start & " " & item.End)
  34. End If
  35. Next
  36. Dim criteria3 As String
  37. criteria3 = "[Start] < " & Chr(34) & VBA.Format(Date + 1, "yyyy-mm-dd hh:nn") & Chr(34) & " AND [Start] >= " & Chr(34) & VBA.Format(Date, "yyyy-mm-dd hh:nn") & Chr(34)
  38. Debug.Print
  39. Debug.Print criteria3
  40. Set todayAppointments = calendarItems.Restrict(criteria3)
  41. ' 用于调试的打印信息
  42. For Each item In todayAppointments
  43. If item.Class = olAppointment Then
  44. Debug.Print (item.subject & " " & item.ConversationTopic & " " & item.Organizer & " " & item.Start & " " & item.End)
  45. End If
  46. Next
  47. End Sub
英文:

Add a time component.

  1. Option Explicit &#39; Consider this mandatory
  2. &#39; Tools | Options | Editor tab
  3. &#39; Require Variable Declaration
  4. &#39; If desperate declare as Variant
  5. Private Sub ApptToday()
  6. Dim calendarItems As Items
  7. Dim todayAppointments As Items
  8. Dim item As Object
  9. Set calendarItems = Session.GetDefaultFolder(olFolderCalendar).Items
  10. &#39;Documentation says sort the collection first, then set include recurrences
  11. &#39;https://learn.microsoft.com/en-us/office/vba/api/outlook.items.includerecurrences
  12. calendarItems.Sort &quot;[Start]&quot;
  13. calendarItems.IncludeRecurrences = True
  14. Dim criteria1 As String
  15. criteria1 = &quot;[Start] &lt; &quot; &amp; Chr(34) &amp; VBA.Format(Now + 1, &quot;Short Date&quot;) &amp; Chr(34) &amp; &quot; AND [Start] &gt;= &quot; &amp; Chr(34) &amp; VBA.Format(Now, &quot;Short Date&quot;) &amp; Chr(34)
  16. Debug.Print
  17. Debug.Print criteria1
  18. Set todayAppointments = calendarItems.Restrict(criteria1)
  19. &#39;Print info for debugging
  20. For Each item In todayAppointments
  21. If item.Class = olAppointment Then
  22. Debug.Print (item.subject &amp; &quot; &quot; &amp; item.ConversationTopic &amp; &quot; &quot; &amp; item.Organizer &amp; &quot; &quot; &amp; item.Start &amp; &quot; &quot; &amp; item.End)
  23. End If
  24. Next
  25. Dim criteria2 As String
  26. criteria2 = &quot;[Start] &lt; &quot; &amp; Chr(34) &amp; VBA.Format(Now + 1, &quot;Short Date&quot;) &amp; &quot; 00:00&quot; &amp; Chr(34) &amp; &quot; AND [Start] &gt;= &quot; &amp; Chr(34) &amp; VBA.Format(Now, &quot;Short Date&quot;) &amp; &quot; 00:00&quot; &amp; Chr(34)
  27. Debug.Print
  28. Debug.Print criteria2
  29. Set todayAppointments = calendarItems.Restrict(criteria2)
  30. &#39;Print info for debugging
  31. For Each item In todayAppointments
  32. If item.Class = olAppointment Then
  33. Debug.Print (item.subject &amp; &quot; &quot; &amp; item.ConversationTopic &amp; &quot; &quot; &amp; item.Organizer &amp; &quot; &quot; &amp; item.Start &amp; &quot; &quot; &amp; item.End)
  34. End If
  35. Next
  36. Dim criteria3 As String
  37. criteria3 = &quot;[Start] &lt; &quot; &amp; Chr(34) &amp; VBA.Format(Date + 1, &quot;yyyy-mm-dd hh:nn&quot;) &amp; Chr(34) &amp; &quot; AND [Start] &gt;= &quot; &amp; Chr(34) &amp; VBA.Format(Date, &quot;yyyy-mm-dd hh:nn&quot;) &amp; Chr(34)
  38. Debug.Print
  39. Debug.Print criteria3
  40. Set todayAppointments = calendarItems.Restrict(criteria3)
  41. &#39;Print info for debugging
  42. For Each item In todayAppointments
  43. If item.Class = olAppointment Then
  44. Debug.Print (item.subject &amp; &quot; &quot; &amp; item.ConversationTopic &amp; &quot; &quot; &amp; item.Organizer &amp; &quot; &quot; &amp; item.Start &amp; &quot; &quot; &amp; item.End)
  45. End If
  46. Next
  47. End Sub

答案2

得分: 0

需要以相反的顺序设置属性。不要首先对集合进行排序,而是首先包括重复项:

  1. calendarItems.Sort "[Start]"```
  2. 尽管日期和时间通常以日期格式存储,但使用 Jet 和 DAV Searching and Locating (DASL) 语法的筛选器要求将日期时间值转换为字符串表示。在 Jet 语法中,日期时间比较字符串应该用双引号或单引号括起来。在 DASL 语法中,日期时间比较字符串应该用单引号括起来。
  3. 为确保日期时间比较字符串格式与 Microsoft Outlook 期望的格式一致,使用 Visual Basic for Applications 的 `Format` 函数。以下示例创建了一个 Jet 筛选器,以查找在2022年6月12日下午3:30之前修改的所有项目,本地时间。
  4. ```criteria = "[LastModificationTime] < '&#" & Format("6/12/2022 3:30PM","General Date") & "#'"```
  5. 在我为技术博客写的文章中了解更多信息:
  6. - [如何在 Outlook 中使用 Restrict 方法获取日历项][1]
  7. - [如何使用 Find 和 FindNext 方法检索 Outlook 日历项][2]
  8. [1]: https://www.add-in-express.com/creating-addins-blog/2011/11/15/outlook-restrict-calendar-items/
  9. [2]: https://www.add-in-express.com/creating-addins-blog/2011/11/11/outlook-retrieve-calendar-items/
  10. <details>
  11. <summary>英文:</summary>
  12. You need to set properties in the reverse order. Instead of sorting the collection first you need to include recurrences first of all:

calendarItems.IncludeRecurrences = True
calendarItems.Sort "[Start]"

  1. Although dates and times are typically stored with a date format, filters using the Jet and DAV Searching and Locating (DASL) syntax require that the date-time value to be converted to a string representation. In Jet syntax, the date-time comparison string should be enclosed in either double quotes or single quotes. In DASL syntax, the date-time comparison string should be enclosed in single quotes.
  2. To make sure that the date-time comparison string is formatted as Microsoft Outlook expects, use the Visual Basic for Applications `Format` function. The following example creates a Jet filter to find all items that have been modified before June 12, 2022 at 3:30 P.M local time.

criteria = "[LastModificationTime] < '" _
& Format("6/12/2022 3:30PM","General Date") & "'"

  1. Read more about that in the articles that I wrote for the technical blog:
  2. - [How To: Use Restrict method in Outlook to get calendar items][1]
  3. - [How To: Retrieve Outlook calendar items using Find and FindNext methods][2]
  4. [1]: https://www.add-in-express.com/creating-addins-blog/2011/11/15/outlook-restrict-calendar-items/
  5. [2]: https://www.add-in-express.com/creating-addins-blog/2011/11/11/outlook-retrieve-calendar-items/
  6. </details>

huangapple
  • 本文由 发表于 2023年3月21日 03:03:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75794303.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定