450 字
2 分鐘
Outlook Email Keyword Triggers Windows Notifications? Get It Done with Snoretoast
Conclusion
- Use Outlook VBA + Snoretoast to automatically trigger Windows notifications based on email content/subject.
Where It’s Useful
- When you need to monitor specific emails in real-time (registrations / alerts / task notifications)
- If you don’t want to constantly stare at Outlook
- For engineers with basic VBA experience
- To create a lightweight notification mechanism (without introducing external services)
Steps
1. Configure Outlook to Monitor Inbox
- Bind to Inbox Items to listen for new incoming emails.
- Use the
Items_ItemAddevent to trigger. - This is the entry point for the entire process; without it, nothing will execute automatically.
Private WithEvents Items As Outlook.Items
Private Sub Application_Startup() Dim Ns As Outlook.NameSpace Set Ns = Application.GetNamespace("MAPI") Set Items = Ns.GetDefaultFolder(olFolderInbox).ItemsEnd Sub2. Determine Email Conditions + Extract Key Content
- Check if Subject / Body contains specified keywords.
- Extract specific information from the email (e.g., registration time).
- Avoid extracting forwarded content (terminate directly).
Private Sub Items_ItemAdd(ByVal Item As Object) On Error Resume Next If TypeOf Item Is Outlook.MailItem Then Dim mail As Outlook.MailItem Set mail = Item
Dim signUpLine As String signUpLine = GetLineWithKeyword(mail.Body, "報名時間")
If InStr(LCase(mail.Subject), "testa") > 0 And _ InStr(LCase(mail.Body), "testb") > 0 Then
Call ShowToast(Replace(mail.Subject, " ", ""), signUpLine) End If End IfEnd Sub3. Call Snoretoast to Display Notifications
- Use
Shellto execute an external exe. - Set the title, content, display duration, and image.
- Snoretoast is the core notification tool (https://github.com/KDE/snoretoast).
Private Sub ShowToast(title As String, message As String) Dim cmd As String cmd = "D:\Snoretoast\snoretoast.exe -appID ""OutlookNotify"" -t """ & title & """ -m """ & message & """ -d long -p D:\Snoretoast\01.jpg " Shell cmd, vbHideEnd Sub4. Extract Key Lines from Email (to avoid incorrect content)
- Split the email into lines and search line by line.
- Stop directly if a forwarded message separator is encountered.
- Clear spaces / tabs to prevent formatting interference.
Private Function GetLineWithKeyword(bodyText As String, keyword As String) As String Dim lines() As String Dim i As Long lines = Split(bodyText, vbCrLf)
For i = LBound(lines) To UBound(lines) If InStr(lines(i), "-----Original Message-----") > 0 Or _ InStr(lines(i), "-----轉寄郵件-----") > 0 Then Exit For End If
If InStr(lines(i), keyword) > 0 Then GetLineWithKeyword = Trim(lines(i)) GetLineWithKeyword = Replace(GetLineWithKeyword, " ", "") GetLineWithKeyword = Replace(GetLineWithKeyword, vbTab, "") Exit Function End If Next i
GetLineWithKeyword = "(找不到報名時間)"End FunctionAdditional Notes
- The Snoretoast path must be correct, otherwise, notifications will not appear.
- Outlook must be open, and VBA must be enabled.
- It’s recommended to convert keywords to lowercase for comparison to avoid case sensitivity issues.
Command / Example Summary
Click to expend
snoretoast.exe -appID "OutlookNotify" -t "Title" -m "Message" -d long -p image.jpgConclusion
- This approach is a bit “rough around the edges”, but it’s stable, controllable, and suitable for internal automated notification scenarios.
- It’s recommended to extract keywords into a configuration file for better maintainability.
Outlook Email Keyword Triggers Windows Notifications? Get It Done with Snoretoast
https://joyceowo.github.io/posts/en/23ba78ea09fa81a28219e45692344815/