modGetBookingAppointments.bas

' ============================================================
' Copyright (c) 2025-2026 Peter Bryant (Corylus Business Systems)
'                    and  Andrew Richards (The IT Service Ltd)
' Part of VBAuth - https://vbauth.com
' Licensed under the MIT Licence - see LICENCE.txt
' This copyright notice must not be removed or altered.
' ============================================================

' ============================================================
' modGetBookingAppointments - VBAuth example: List Microsoft Bookings appointments
' ============================================================
' Demonstrates the standard VBAuth call pattern for a GET request
' against a Bookings business calendar.
'
' Admin table record required: AppID = 4
'   ActionName:    List Bookings
'   Verb:          GET
'   URLPart1:      https://graph.microsoft.com/v1.0/solutions/bookingBusinesses/
'   URLParameter1: Bookings calendar SMTP address (e.g. mybusiness@contoso.com)
'   URLPart2:      /appointments
'   ScopeClaims:   BookingsAppointment.ReadWrite.All
'
' Note: returns all appointments for the specified Bookings business.
' Handles Graph API paging automatically via @odata.nextLink.
' ============================================================

' ------------------------------------------------------------
' DEPENDENCIES - required modules in any target database:
'   modVBAuthGlobalConstants  (globals, SetEntraAPPIdValues,
'                              AcquireBearerToken)
'   JSONConverter              (Tim Hall VBA-JSON parser)
' ------------------------------------------------------------
' CALLING EXAMPLE (no auth code required):
'   Dim strList As String
'   Dim lngCount As Long
'   If GetBookingAppointments(strList, lngCount, "calendar@contoso.com") Then
'       ' strList contains formatted appointment lines
'   End If


Option Compare Database
Option Explicit

Public Function GetBookingAppointments(ByRef strAppointmentList As String, _
                                       ByRef lngAppointmentCount As Long, _
                                       ByVal strBusinessID As String) As Boolean

Dim http                As Object
Dim strResponse         As String
Dim objJson             As Object
Dim objAppointments     As Object
Dim objAppt             As Object
Dim strNextLink         As String
Dim strURL              As String
Dim strStart            As String
Dim strCustomer         As String
Dim strService          As String
Dim strLine             As String

    GetBookingAppointments = False
    strAppointmentList = ""
    lngAppointmentCount = 0

    On Error GoTo ErrorHandler

    intAppID = 6
    blnResult = SetEntraAPPIdValues(True, False, strBusinessID, "")
    If blnResult = False Then
        strAppointmentList = strMessage
        Exit Function
    End If
    blnResult = AcquireBearerToken(strClientID, strTenantID, strScopeRequested)
    If blnResult = False Then
        strAppointmentList = strMessage
        Exit Function
    End If

    strURL = strGraphAPI_URL
    'change url to filter for the next month
    strURL = strGraphAPI_URL & "?$top=50&$filter=startDateTime/dateTime ge '" & _
         Format(Now(), "yyyy-mm-dd") & "T00:00:00' and startDateTime/dateTime le '" & _
         Format(DateAdd("d", 2, Now()), "yyyy-mm-dd") & "T00:00:00'"

    Do
        Set http = CreateObject("MSXML2.ServerXMLHTTP.6.0")
        http.Open strGraphAPI_Verb, strURL, False
        http.setRequestHeader "Authorization", "Bearer " & strBearerToken
        http.setRequestHeader "Accept", "application/json"
        http.setTimeouts lngHTTPTimeout, lngHTTPTimeout, lngHTTPTimeout, lngHTTPTimeout
        http.Send
        
        Debug.Print "HTTP Status: " & http.status
        strResponse = http.ResponseText
        Debug.Print strResponse

        If http.status <> 200 Then
            strAppointmentList = "HTTP " & http.status & ": " & http.ResponseText
            Exit Function
        End If

        Set objJson = JSONConverter.ParseJson(strResponse)
        Set objAppointments = objJson("value")

        For Each objAppt In objAppointments
            ' Skip non-Bookings appointments (regular diary entries have no customer)
            strCustomer = ""
            On Error Resume Next
            strCustomer = objAppt("customerName")
            On Error GoTo ErrorHandler
            If Trim(strCustomer) = "" Then
                GoTo NextAppointment
            End If
        
        
        
            ' Start date/time
            strStart = ""
            On Error Resume Next
            strStart = objAppt("startDateTime")("dateTime")
            On Error GoTo ErrorHandler
            If strStart <> "" Then
                strStart = Mid(strStart, 9, 2) & "/" & Mid(strStart, 6, 2) & "/" & Left(strStart, 4) & _
                           " " & Mid(strStart, 12, 5)
            End If
            
            ' Customer name
            strCustomer = ""
            On Error Resume Next
            strCustomer = objAppt("customerName")
            On Error GoTo ErrorHandler

            ' Service name
            strService = ""
            On Error Resume Next
            strService = objAppt("serviceName")
            On Error GoTo ErrorHandler

            strLine = strStart & "  |  " & strCustomer & "  |  " & strService
            strAppointmentList = strAppointmentList & strLine & vbCrLf
            lngAppointmentCount = lngAppointmentCount + 1
NextAppointment:
        Next objAppt

        ' Check for next page
        strNextLink = ""
        On Error Resume Next
        strNextLink = objJson("@odata.nextLink")
        On Error GoTo ErrorHandler

        strURL = strNextLink

    Loop While strNextLink <> ""

    ' Trim trailing line break
    If Len(strAppointmentList) > 0 Then
        strAppointmentList = Left(strAppointmentList, Len(strAppointmentList) - 2)
    End If

    GetBookingAppointments = True
    Exit Function

ErrorHandler:
    If Err.Number = -2147012894 Then
        strAppointmentList = "The request to Microsoft Graph timed out after " & _
                             lngHTTPTimeout / 1000 & " seconds. " & _
                             "Please check your network connection and try again."
    Else
        strAppointmentList = "Request failed: " & Err.Number & " - " & Err.Description
    End If

End Function
Code language: PHP (php)
Updated on April 4, 2026