如何从Excel中的给定日期获取Outlook约会

时间:2019-07-15 18:15:00

标签: excel vba outlook outlook-vba

我想在MessageBox中显示给定日期的Outlook日历约会。不幸的是,我使用的代码今天没有显示任何约会。如果我将代码更改为 sfilter = "[Start] >= '" & startDate & "' " 然后我得到今天的约会,以及其他约会的所有未来约会。我只想显示指定日期的约会。

日期选择来自UserForm的{​​{1}}

cmDates.srtDate.Value是我在整个代码中使用保留日期过滤器的变量

代码

sFilter

2 个答案:

答案 0 :(得分:0)

您的限制应包含两个部分-Start > today's midnightStart < tomorrow's midnight。您只有第一部分。

还要记住,如果您想要重复活动的实例(而不仅仅是主约会),则需要使用Items.IncludeRecurrences属性-请参见https://docs.microsoft.com/en-us/office/vba/api/outlook.items.includerecurrences

答案 1 :(得分:0)

有几个方面:

  1. 要从符合预定义条件的文件夹中检索所有Outlook约会项目,您需要按升序sort并将IncludeRecurrences设置为true。如果您在使用Restrict方法之前不执行此操作,则不会捕获定期约会!
  2. 如果您设置了IncludeRecurrences属性,Microsoft建议不要使用Count属性。 Count属性可能会返回意外结果并引起无限循环。
  3. 尽管日期和时间通常以Date格式存储,但是FindRestrict方法要求将日期和时间转换为字符串表示形式。要确保日期格式符合Microsoft Outlook的预期格式,请使用VBA中提供的Format函数。因此,您必须以Outlook可以理解的格式指定日期。
     Format(youDate, "ddddd h:nn AMPM")

例如,这是示例VB.NET代码:

Imports System.Text
Imports System.Diagnostics
' ...
Private Sub RestrictCalendarItems(folder As Outlook.MAPIFolder)
    Dim dtEnd As DateTime = New DateTime(DateTime.Now.Year, DateTime.Now.Month, _
                                         DateTime.Now.Day, 23, 59, 0, 0)
    Dim restrictCriteria As String = "[Start]<=""" + dtEnd.ToString("g") + """" + _
                                     " AND [End]>=""" + DateTime.Now.ToString("g") + """"
    Dim strBuilder As StringBuilder = Nothing
    Dim folderItems As Outlook.Items = Nothing
    Dim resultItems As Outlook.Items = Nothing
    Dim appItem As Outlook._AppointmentItem = Nothing
    Dim counter As Integer = 0
    Dim item As Object = Nothing
    Try
        strBuilder = New StringBuilder()
        folderItems = folder.Items
        folderItems.IncludeRecurrences = True
        folderItems.Sort("[Start]")
        resultItems = folderItems.Restrict(restrictCriteria)
        item = resultItems.GetFirst()
        Do
            If Not IsNothing(item) Then
                If (TypeOf (item) Is Outlook._AppointmentItem) Then
                    counter = counter + 1
                    appItem = item
                    strBuilder.AppendLine("#" + counter.ToString() + _
                                          " Start: " + appItem.Start.ToString() + _
                                          " Subject: " + appItem.Subject + _
                                          " Location: " + appItem.Location)
                End If
                Marshal.ReleaseComObject(item)
                item = resultItems.GetNext()
            End If
        Loop Until IsNothing(item)
        If (strBuilder.Length > 0) Then
            Debug.WriteLine(strBuilder.ToString())
        Else
            Debug.WriteLine("There is no match in the " _
                             + folder.Name + " folder.")
        End If
    catch ex As Exception
        System.Windows.Forms.MessageBox.Show(ex.Message)
    Finally
        If Not IsNothing(folderItems) Then Marshal.ReleaseComObject(folderItems)
        If Not IsNothing(resultItems) Then Marshal.ReleaseComObject(resultItems)
    End Try
End Sub

您可能会发现以下文章有帮助: