565 字
3 分鐘
瀏覽次數
Outlook 信件關鍵字觸發 Windows 通知?用 Snoretoast 一次搞定
2025-07-25
2026-04-10

結論#

  • 用 Outlook VBA + Snoretoast,可針對信件內容/主旨自動觸發 Windows 通知

適合用在哪裡#

  • 需要即時關注特定信件(報名 / 告警 / 任務通知)
  • 不想一直盯 Outlook
  • 有簡單 VBA 經驗的工程師
  • 想做輕量級通知機制(不引入外部服務)

流程步驟#

1. 設定 Outlook 監聽收件匣#

  • 綁定 Inbox Items,監聽新信件進來
  • 使用 Items_ItemAdd 事件觸發
  • 這是整個流程的入口,沒這段就不會自動執行
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).Items
End Sub

2. 判斷信件條件 + 抓關鍵內容#

  • 檢查 Subject / Body 是否包含指定關鍵字
  • 從信件內抓指定資訊(例如:報名時間)
  • 避免抓到轉寄內容(直接中斷)
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 If
End Sub

3. 呼叫 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, vbHide
End Sub

4. 抽取信件關鍵行(避免抓錯內容)#

  • 將信件拆行後逐行搜尋
  • 遇到轉寄分隔直接停止
  • 清除空白 / Tab 避免格式干擾
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 Function

補充#

  • Snoretoast 路徑需正確,否則不會跳通知
  • Outlook 需開啟且 VBA 有啟用
  • 關鍵字建議轉小寫比對,避免大小寫問題

指令 / 範例整理#

Click to expend
Terminal window
snoretoast.exe -appID "OutlookNotify" -t "Title" -m "Message" -d long -p image.jpg

收尾#

  • 這做法很土,但穩、可控,適合內部自動通知場景
  • 建議把關鍵字抽成設定檔,會更好維護
Outlook 信件關鍵字觸發 Windows 通知?用 Snoretoast 一次搞定
https://joyceowo.github.io/posts/23ba78ea09fa81a28219e45692344815/
作者
JoyceOwO
發佈於
2025-07-25
許可協議
CC BY-NC-SA 4.0