' ============================================================
' Copyright (c) 2025-2026 Peter Bryant (Corylus Business Systems)
' and Andrew Richards (The IT Service Ltd)
' Part of VBAuth - https:
' Licensed under the MIT Licence - see LICENCE.txt
' This copyright notice must not be removed or altered.
' ============================================================
' ============================================================
' modValidateContacts - VBAuth example: Check email addresses
' against the authenticated user's Exchange contacts via
' Microsoft Graph API.
' ============================================================
' AppID = 3
' ActionName: Check Contact Exists
' Verb: GET
' URLPart1: https://graph.microsoft.com/v1.0/me/contacts
' ?$filter=emailAddresses/any(e:e/address eq '
' URLParameter1: email address to check
' URLPart2: ')&$select=displayName,emailAddresses&$top=1
' ScopeClaims: Mail.Send,Contacts.Read
' ClientID: same as AppID=2 (Send Email)
' TenantID: same as other records
'
' NOTE: AppID=3 record must exist in lclVBAuthAdmin before use.
' Insert it manually or via the Immediate Window using the
' INSERT statement in the header comment below.
'
' INSERT INTO lclVBAuthAdmin
' (AppID, ActionName, Verb, URLPart1, URLParameter1, URLPart2,
' ScopeClaims, ClientID, TenantID)
' VALUES
' (3, 'Check Contact Exists', 'GET',
' 'https://graph.microsoft.com/v1.0/me/contacts?$filter=emailAddresses/any(e:e/address eq ''',
' 'EmailAddress',
' ''')&$select=displayName,emailAddresses&$top=1',
' 'Mail.Send,Contacts.Read',
' <copy from AppID=2>,
' <copy from any existing record>)
' ============================================================
' ------------------------------------------------------------
' DEPENDENCIES - required modules in any target database:
' modVBAuthGlobalConstants (globals, SetEntraAPPIdValues,
' AcquireBearerToken)
' JSONConverter (Tim Hall VBA-JSON parser)
' ------------------------------------------------------------
' CALLING EXAMPLE (no auth code required):
' Dim strMsg As String
' If ValidateContactAddresses(
' "to@example.com", "", "", strMsg) Then
' MsgBox strMsg ' "All addresses found" or not-found list
' End If
Option Compare Database
Option Explicit
' ============================================================
' Main public function - called from cmdValidateContacts_Click
'
' Checks all addresses in To, CC and BCC against Graph contacts.
' strMessage returns either "All addresses found in contacts."
' or a formatted list of not-found addresses grouped by field.
' Returns False only if authentication or API call fails.
' ============================================================
Public Function ValidateContactAddresses(ByVal strTo As String, _
ByVal strCC As String, _
ByVal strBCC As String, _
ByRef strMessage As String) As Boolean
Dim strToMissing As String
Dim strCCMissing As String
Dim strBCCMissing As String
Dim strResult As String
ValidateContactAddresses = False
strMessage = ""
On Error GoTo ErrorHandler
strToMissing = ""
strCCMissing = ""
strBCCMissing = ""
intAppID = 3
If Not SetEntraAPPIdValues(False, False, "", "") Then
strMessage = "Setup failed for AppID=3: " & strMessage
Exit Function
End If
If Not AcquireBearerToken(strClientID, strTenantID, strScopeRequested) Then
strMessage = "Could not acquire token: " & strMessage
Exit Function
End If
If Not CheckAddressList(strTo, strToMissing, strMessage) Then
Exit Function
End If
If Not CheckAddressList(strCC, strCCMissing, strMessage) Then
Exit Function
End If
If Not CheckAddressList(strBCC, strBCCMissing, strMessage) Then
Exit Function
End If
' Build result message
If strToMissing = "" And strCCMissing = "" And strBCCMissing = "" Then
strMessage = "All addresses found in your personal Exchange contacts."
Else
strResult = "The following addresses were not found in your personal Exchange contacts:" & vbCrLf & vbCrLf & _
"(Note: this checks your Exchange mailbox contacts folder only, not the Global Address List.)" & vbCrLf & vbCrLf
If strToMissing <> "" Then
strResult = strResult & "To:" & vbCrLf & strToMissing & vbCrLf
End If
If strCCMissing <> "" Then
strResult = strResult & "CC:" & vbCrLf & strCCMissing & vbCrLf
End If
If strBCCMissing <> "" Then
strResult = strResult & "BCC:" & vbCrLf & strBCCMissing & vbCrLf
End If
strResult = strResult & vbCrLf & _
"Note: this is advisory only. You can still send to these addresses."
strMessage = strResult
End If
ValidateContactAddresses = True
Exit Function
ErrorHandler:
If Err.Number = -2147012894 Then
strMessage = "The request to Microsoft Graph timed out after " & _
lngHTTPTimeout / 1000 & " seconds. " & _
"Please check your network connection and try again."
Else
strMessage = "Validation failed: " & Err.Number & " - " & Err.Description
End If
End Function
' ============================================================
' Private: check a comma-separated address list against contacts
' Appends any not-found addresses to strNotFound as bullet lines
' Returns False if an API or auth error occurred
' ============================================================
Private Function CheckAddressList(ByVal strList As String, _
ByRef strNotFound As String, _
ByRef strErrorMessage As String) As Boolean
Dim arrAddresses() As String
Dim strAddress As String
Dim blnFound As Boolean
Dim i As Integer
CheckAddressList = True
strList = Trim(strList)
If strList = "" Then
Exit Function
End If
arrAddresses = Split(Replace(strList, ";", ","), ",")
For i = 0 To UBound(arrAddresses)
strAddress = Trim(arrAddresses(i))
If strAddress <> "" Then
If Not ContactExists(strAddress, blnFound, strErrorMessage) Then
' Auth or HTTP error - abort entire check
CheckAddressList = False
Exit Function
End If
If Not blnFound Then
strNotFound = strNotFound & " " & strAddress & vbCrLf
End If
End If
Next i
End Function
' ============================================================
' Private: check a single email address against Graph contacts
' blnFound returns True if contact exists, False if not found
' Returns False on auth or HTTP error, True on clean result
' ============================================================
Private Function ContactExists(ByVal strEmail As String, _
ByRef blnFound As Boolean, _
ByRef strErrorMessage As String) As Boolean
Dim http As Object
Dim strResponse As String
Dim objJson As Object
Dim objValue As Object
Dim strEncodedEmail As String
Dim strURL As String
ContactExists = False
blnFound = False
On Error GoTo ErrorHandler
strEncodedEmail = Replace(strEmail, "'", "%27")
strEncodedEmail = Replace(strEncodedEmail, "@", "%40")
' Build URL directly - token already acquired by caller
strURL = Nz(DLookup("URLPart1", "lclVBAuthAdmin", "AppID = 3"), "") & _
strEncodedEmail & _
Nz(DLookup("URLPart2", "lclVBAuthAdmin", "AppID = 3"), "")
If strURL = "" Then
strErrorMessage = "Could not build contact check URL from lclVBAuthAdmin AppID=3"
Exit Function
End If
Set http = CreateObject("MSXML2.ServerXMLHTTP.6.0")
http.Open "GET", strURL, False
http.setRequestHeader "Authorization", "Bearer " & strBearerToken
http.setRequestHeader "Accept", "application/json"
http.setTimeouts lngHTTPTimeout, lngHTTPTimeout, lngHTTPTimeout, lngHTTPTimeout
http.Send
If http.status <> 200 Then
strErrorMessage = "HTTP " & http.status & ": " & http.ResponseText
Exit Function
End If
strResponse = http.ResponseText
Set objJson = JSONConverter.ParseJson(strResponse)
Set objValue = objJson("value")
blnFound = (objValue.Count > 0)
ContactExists = True
Exit Function
ErrorHandler:
If Err.Number = -2147012894 Then
strErrorMessage = "The request to Microsoft Graph timed out after " & _
lngHTTPTimeout / 1000 & " seconds. " & _
"Please check your network connection and try again."
Else
strErrorMessage = "ContactExists failed: " & Err.Number & " - " & Err.Description
End If
End FunctionCode language: PHP (php)