【问题标题】:finding specific appointments in agenda through excel vba通过 excel vba 在议程中查找特定约会
【发布时间】:2018-01-09 11:04:46
【问题描述】:

我有一个 Excel 文件,我在其中记录了员工的行为。

这是以下设计: ID 数据类型 Typedatail 元数据 Regdata hours

  • 3767 01/04/2018 SN VM 64 05/01/2018 4

  • 3767 01/04/2018 SN NM 65 05/01/2018 4

  • 3767 03/04/2018 SN VM 66 05/01/2018 4

  • 3767 03/04/2018 SN NM 67 05/01/2018 4

  • 3767 04/04/2018 SN VM 68 05/01/2018 4

  • 3767 04/04/2018 SN NM 69 05/01/2018 4

  • 3767 07/04/2018 CA 70 05/01/2018 8

  • 3767 08/04/2018 CA 71 05/01/2018 8

  • 3767 09/04/2018 CA 72 05/01/2018 8

  • 3683 12/01/2018 OU-73 05/01/2018 -8

我还需要将它们放入日历中以分发这些知识。 !(https://ibb.co/hQmOxR)

但有时我需要编辑这些。 (更改或删除那些)

我发现以下内容作为基础 Search Appointments in excel with VBA

这最终会找到他们,但这会贯穿所有约会,因为我知道约会是在哪个日期设置的,所以这是不需要的。

因此我想限制范围,但我犯了一个错误,我无法弄清楚。

基本查找约会

    Public Function CheckAppointment(ByVal argCheckDate As Date, ByVal argTikNummer As Integer) As Boolean

    Const olAppointment = 26 ' <== Added this line and your code worked.
    Dim oApp As Object
    Dim oNameSpace As Object
    Dim oApptItem As Object
    Dim oFolder As Object
    Dim oMeetingoApptItem As Object
    Dim oObject As Object

    Dim strRestriction As String        'opmaak zoekbeperking


    On Error Resume Next ' No appointment was found since you have this line and olAppointmnet wasn't defined.

    Set oApp = GetObject(, "Outlook.Application")
    If oApp Is Nothing Then Set oApp = CreateObject("Outlook.Application")

    Set oNameSpace = oApp.GetNamespace("MAPI")
    Set oFolder = oApp.GetNamespace("MAPI").GetDefaultFolder(olFolderCalendar) 'oNameSpace.Session.GetDefaultFolder(9).Folders(olFolderCalendar)
    CheckAppointment = False
    For Each oObject In oFolder.Items
        'MsgBox oObject
        If (oObject.Class = olAppointment) Then ' <== This is why you need to define it first
            Set oApptItem = oObject

            If oApptItem.Start = argCheckDate And InStr(oApptItem.Body, argTikNummer) Then
                MsgBox oApptItem.Body
                CheckAppointment = True
                Exit For ' <== Added this exit for loop to improve performance
            End If
        End If
    Next oObject

    Set oApp = Nothing
    Set oNameSpace = Nothing
    Set oApptItem = Nothing
    Set oFolder = Nothing
    Set oObject = Nothing
End Function

Public Sub Driver()
    Dim dtCheck As Date
    Dim intTikNummer As Integer
    Dim sbCheck As String
Sheets("blad1").Select
Dim i As Long
i = 2


    Do Until Trim(Cells(i, 1).Value) = ""                                   'voorlopig test omgeving. Moet worden omgevormd tot een single entry test
        dtCheck = Cells(i, 2) '+ TimeValue("09:00:00")
        intTikNummer = Cells(i, 1)
        If CheckAppointment(dtCheck, intTikNummer) Then
            MsgBox "Appointment found", vbOKOnly + vbInformation            'dummy uitkomst verslag. Moet worden vervangen door een opdracht
        Else
            MsgBox "Appointment not found", vbOKOnly + vbExclamation        'dummy uitkomst verslag. Moet worden vervangen door een opdracht
        End If
    i = i + 1
    Loop
End Sub



`

基本限制示例(展望) 子 FindAppts()

        Dim myStart As Date
        Dim myEnd As Date
        Dim oCalendar As Outlook.folder
        Dim oItems As Outlook.items
        Dim oItemsInDateRange As Outlook.items
        Dim oFinalItems As Outlook.items
        Dim oAppt As Outlook.AppointmentItem
        Dim strRestriction As String

        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
        Set oCalendar = Application.session.GetDefaultFolder(olFolderCalendar)
        Set oItems = oCalendar.items
        oItems.IncludeRecurrences = True
        oItems.Sort "[Start]"
        'Restrict the Items collection for the 30-day date range
        Set oItemsInDateRange = oItems.Restrict(strRestriction)
        'Construct filter for Subject containing 'team'
        Const PropTag  As String = "http://schemas.microsoft.com/mapi/proptag/"
        strRestriction = "@SQL=" &; Chr(34) &; PropTag _
            &; "0x0037001E" &; Chr(34) &; " like '%team%'"
        'Restrict the last set of filtered items for the subject
        Set oFinalItems = oItemsInDateRange.Restrict(strRestriction)
        'Sort and Debug.Print final results
        oFinalItems.Sort "[Start]"
        For Each oAppt In oFinalItems
            Debug.Print oAppt.Start, oAppt.Subject
        Next
    End Sub

`

我得出以下结果(通过示例)

Public Function CheckAppointment(ByVal argCheckDate As Date, ByVal argTikNummer As Integer) As Boolean

    Const olAppointment = 26 ' <== Added this line and your code worked.
    Dim oApp As Object
    Dim oNameSpace As Object
    Dim oApptItem As Object
    Dim oFolder As Object
    Dim oFolderA As Object
    Dim oFolderB As Object
    Dim oMeetingoApptItem As Object
    Dim oObject As Object

    Dim myStart, myEnd As Date
    Dim strRestriction As String        'opmaak zoekbeperking
    'Construct filter for day date range
    myStart = Format(argCheckDate, "dd/mm/yyyy") 'argcheckdate
    myEnd = DateAdd("d", 1, myStart)
    myEnd = Format(myEnd, "dd/mm/yyyy")  'Argcheckdate
    Debug.Print "Start:", myStart
    Debug.Print "End:", myEnd

    strRestriction = "[Start] = '" & myStart & "' AND [End] = '" & myEnd & "'"



    On Error Resume Next ' No appointment was found since you have this line and olAppointmnet wasn't defined.

    Set oApp = GetObject(, "Outlook.Application")
    If oApp Is Nothing Then Set oApp = CreateObject("Outlook.Application")

    Set oNameSpace = oApp.GetNamespace("MAPI")
    Set oFolder = oApp.GetNamespace("MAPI").GetDefaultFolder(olFolderCalendar) 'oNameSpace.Session.GetDefaultFolder(9).Folders(olFolderCalendar)
    CheckAppointment = False

    'Restrict the Items collection for the 30-day date range
    Set oFolderA = oFolder.Restrict(strRestriction)


    For Each oObject In oFolderA.Items
        MsgBox oObject & " : " & oObject.Start & " : " & myStart & " - " & myEnd
        If (oObject.Class = olAppointment) Then ' <== This is why you need to define it first
            Set oApptItem = oObject

            If oApptItem.Start = argCheckDate And InStr(oApptItem.Body, argTikNummer) Then
                MsgBox oApptItem.Body
                CheckAppointment = True
                Exit For ' <== Added this exit for loop to improve performance
            End If
        End If
    Next oObject

    Set oApp = Nothing
    Set oNameSpace = Nothing
    Set oApptItem = Nothing
    Set oFolder = Nothing
    Set oFolderA = Nothing
    Set oFolderB = Nothing
    Set oObject = Nothing
End Function

我最终得到一个空的 oApptItem 但如果 oApptItem.Start = argCheckDateInStr(oApptItem.Body, argTikNummer) 触发 true,尽管约会不在列表中。

上面的代码是用来编辑或删除约会的基础。

【问题讨论】:

    标签: vba excel outlook automation appointment


    【解决方案1】:

    过滤日历很棘手,而且不关闭On Error Resume Next 会使情况更加复杂。

    Option Explicit
    
    Private Sub CheckAppointment_test()
    
        ' Appointment date
        Dim argCheckDate As String
        argCheckDate = "2018-01-05"
    
        ' Text in body of appointment
        Dim argTikNummer As Long
        argTikNummer = 3767
    
        Dim result As Boolean
        MsgBox CheckAppointment(argCheckDate, argTikNummer) & vbCr & " Done."
    
    End Sub
    
    
    Public Function CheckAppointment(ByVal argCheckDate As Date, ByVal argTikNummer As Integer) As Boolean
    
        ' Not required as Outlook must be referenced
        '  to use olFolderCalendar later
        'Const olAppointment = 26
    
        Dim oApp As Object
        Dim oNameSpace As Object
        Dim oApptItem As Object
    
        Dim oFolder As Object
        Dim oFolderA As Object
    
        Dim oObject As Object
    
        Dim myStart, myEnd As Date
        Dim strRestriction As String
    
        'Construct filter for day date range
        'myStart = Format(argCheckDate, "dd/mm/yyyy") 'argcheckdate
        myStart = Format(argCheckDate, "yyyy-mm-dd") 'argcheckdate
    
        myEnd = DateAdd("d", 1, myStart)
        'myEnd = Format(myEnd, "dd/mm/yyyy")  'Argcheckdate
        myEnd = Format(myEnd, "yyyy-mm-dd")  'Argcheckdate
    
        Debug.Print "Start:", myStart
        Debug.Print "End:", myEnd
    
        'strRestriction = "[Start] = '" & myStart & "' AND [End] = '" & myEnd & "'"
        strRestriction = "[Start] <= '" & myEnd & "' AND [End] >= '" & myStart & "'"
        Debug.Print strRestriction
    
        ' Misuse causes insurmountable problems as errors are hidden
        On Error Resume Next
        Set oApp = GetObject(, "Outlook.Application")
        ' Mandatory with On Error Resume Next to stop bypassing errors
        On Error GoTo 0
    
        ' Handle error bypassed above, if any
        If oApp Is Nothing Then Set oApp = CreateObject("Outlook.Application")
    
        Set oNameSpace = oApp.GetNamespace("MAPI")
        Set oFolder = oNameSpace.GetDefaultFolder(olFolderCalendar)
    
        CheckAppointment = False
    
    
    
        ' *******
    
        Dim oFolderItems As Object
        Set oFolderItems = oFolder.items
    
        ' Strange behaviour if this is not used
        oFolderItems.IncludeRecurrences = True
    
        oFolderItems.Sort "[Start]"
    
        ' *******
    
    
        'Restrict the Items collection for the specified date range
        Set oFolderA = oFolderItems.Restrict(strRestriction)
    
        'For Each oObject In oFolderA.items
        For Each oObject In oFolderA
    
            Debug.Print oObject.Subject & " : " & oObject.Start
    
            If (oObject.Class = olAppointment) Then
    
                Set oApptItem = oObject
    
                Debug.Print "oApptItem.Start: " & oApptItem.Start
                Debug.Print "Formatted oApptItem.Start: " & Format(oApptItem.Start, "yyyy-mm-dd")
                Debug.Print "argCheckDate: " & argCheckDate
    
                ' Not true unless oApptItem.Start is formatted to match format of argCheckDate
                '  Should not be necessary if the filter is working correctly
                'If oApptItem.Start = argCheckDate Then
    
                    Debug.Print InStr(oApptItem.body, argTikNummer)
                    If InStr(oApptItem.body, argTikNummer) Then
                        MsgBox oApptItem.Subject & vbCr & oApptItem.body
                        CheckAppointment = True
    
                        ' Do not exit if there can be multiple appointments
                        'Exit For
    
                    End If
    
                'End If
    
            End If
    
        Next oObject
    
        Set oApp = Nothing
        Set oNameSpace = Nothing
        Set oApptItem = Nothing
        Set oFolder = Nothing
        Set oFolderA = Nothing
        Set oObject = Nothing
    
    End Function
    

    【讨论】:

    • 这段代码有效。我从中得到一个结果。但它有一些特别之处,我还不能确定。假设我在 18 日检查。我期望并且需要得到结果才能找到前一天和/或第二天。 17号有参赛作品
    • 17 号有一个条目我找不到,19 号有一个条目我找到了。
    • 在注释掉argCheckDate = "2018-01-17"myEnd = DateAdd("d", 3, myStart)Exit For 后,您应该会找到从17 日到19 日的所有实例。
    • 感谢您的评论。我知道那将是一个解决方法。但问题是,为什么我能找到 19 号的约会,却找不到 17 号的约会,即使出口已被涂抹。
    • 似乎按我的预期工作。我认为这取决于您进行调试,在约会或逻辑中找到问题。您可以在网站上搜索具有不同解决方案的问题。一些在这里收集stackoverflow.com/questions/tagged/outlook-filter
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-25
    • 2013-02-02
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    相关资源
    最近更新 更多