List Unread Emails via Microsoft Graph (VBA) #
What this does #
Retrieves the 20 most recent or 20 oldest unread emails from either the signed-in user’s Inbox or their entire mailbox, using the Microsoft Graph v1.0 messages endpoint. Authentication is handled by VBAuth using OAuth 2.0 with MFA support. The user selects the search scope (Inbox only or all folders) and sort order (most recent or oldest) via option buttons before running the query. Results are displayed in a columnar text box showing sender name, subject, and received date.
Office apps #
- Access (tested M365)
- Excel (untested M365)
Required components #
- VBAuth.dll v1.x
- modVBAuthGlobalConstants.bas (from /shared)
- modListUnreadEmails.bas (this module)
Azure Entra App Registration #
Delegated permissions:
- Mail.Read
VBAuth Admin Table – Required Values #
AppID=14 — Inbox Only #
| Column Name | Required Value / Example | Notes |
|---|---|---|
| AppID | 14 | Identifies this feature’s configuration record (Inbox) |
| ActionName | VBAuth – List Unread Emails (Inbox) | Display name — any value acceptable |
| ClientID | (your App Client ID) | GUID from Azure App Registration Overview blade |
| TenantID | (your Tenant ID) | GUID from Azure App Registration Overview blade |
| ObjectID | (your Object ID) | Set by PopulateMissingObjectIDs — shared registration with AppID=15 |
| ScopeClaims | Mail.Read | Comma-separated Graph scopes — shared registration with AppID=15 |
| Verb | GET | HTTP method |
| URLPart1 | https://graph.microsoft.com/v1.0/me/mailFolders/Inbox/messages | Graph endpoint |
| URLParameter1 | NO VALUE REQUIRED | Not used |
| URLPart2 | NO VALUE REQUIRED | Not used |
| URLParameter2 | NO VALUE REQUIRED | Not used |
| URLPart3 | NO VALUE REQUIRED | Not used |
| WaitForLogin | 60 | Seconds — NB MSXML2 timeout parameters are in milliseconds |
AppID=15 — All Folders #
| Column Name | Required Value / Example | Notes |
|---|---|---|
| AppID | 15 | Identifies this feature’s configuration record (All Folders) |
| ActionName | VBAuth – List Unread Emails (All Folders) | Display name — any value acceptable |
| ClientID | (your App Client ID) | GUID from Azure App Registration Overview blade — same registration as AppID=14 |
| TenantID | (your Tenant ID) | GUID from Azure App Registration Overview blade — same as AppID=14 |
| ObjectID | (your Object ID) | Set by PopulateMissingObjectIDs — same as AppID=14 |
| ScopeClaims | Mail.Read | Comma-separated Graph scopes — shared registration with AppID=14 |
| Verb | GET | HTTP method |
| URLPart1 | https://graph.microsoft.com/v1.0/me/messages | Graph endpoint |
| URLParameter1 | NO VALUE REQUIRED | Not used |
| URLPart2 | NO VALUE REQUIRED | Not used |
| URLParameter2 | NO VALUE REQUIRED | Not used |
| URLPart3 | NO VALUE REQUIRED | Not used |
| WaitForLogin | 60 | Seconds — NB MSXML2 timeout parameters are in milliseconds |
Module: modListUnreadEmails.bas #
Public functions #
Public Function ListUnreadEmails(ByVal intScope As Integer, ByVal intOrder As Integer) As String
Code language: PHP (php)
Calls the appropriate Graph endpoint based on intScope (1 = Inbox, 2 = all folders) and returns up to 20 unread messages sorted by intOrder (1 = most recent first, 2 = oldest first). OData parameters $filter=isRead eq false, $orderby=receivedDateTime, $top=20, and $select=from,subject,receivedDateTime are appended to the URL at runtime. Returns a formatted columnar string on success, or a string prefixed with "ERROR:" on failure.
Private functions #
Private Function PadR(ByVal strText As String, ByVal intWidth As Integer) As String
Code language: JavaScript (javascript)
Right-pads or truncates a string to exactly intWidth characters. Strings longer than intWidth are truncated and suffixed with ....
Behaviour #
- Runs as the signed-in user (delegated permissions — no application permissions supported)
- HTTP 200 OK indicates success
- OData query parameters are appended to
strGraphAPI_URLafterSetEntraAPPIdValuessets it — they are not stored in lclVBAuthAdmin - AppID 14 is used when Inbox only is selected; AppID 15 when All folders is selected
- Both AppIDs share the same ClientID, TenantID, ObjectID, and ScopeClaims — one Azure app registration serves both
- Sender display name is used if present; email address is used as fallback
- Received date is displayed as yyyy-MM-dd (date portion of ISO 8601 timestamp only)
- HTTP timeouts use the standard
lngHTTPTimeoutconstant from modVBAuthGlobalConstants - Interactive sign-in is required if no cached token exists
Notes #
- No application permissions — delegated only; the signed-in user must have a mailbox
- Mail.Read is sufficient — Mail.ReadWrite is not required
- Admin consent is not required for Mail.Read in most tenant configurations, but may be required depending on tenant policy
- The
isRead eq falsefilter and$orderby/$topare applied server-side — no client-side filtering is performed - When
$filterand$orderbyare combined, the ordered property (receivedDateTime) must not conflict with the filter property ordering rules — this query is constructed correctly and will not trigger the “restriction or sort order is too complex” error - Configure button writes ClientID, TenantID, and ObjectID to both AppID 14 and AppID 15 simultaneously
VBAuth, developed and maintained by Andrew Richards (The IT Service Ltd) and Peter Bryant (Corylus Business Systems), © 2025–2026. All rights reserved. https://vbauth.com