如何在Outlook中自動向生日為今天的聯絡人發送祝福訊息?
有時候,您可能希望在Outlook中當聯絡人的生日為今天時,自動向其發送祝福訊息。如果逐一檢查聯絡人的生日並手動發送祝福郵件,這將是一項繁瑣的工作。本文將介紹一種VBA代碼來快速輕鬆地解決這個問題。
使用Outlook中的VBA代碼根據生日自動向聯絡人發送祝福訊息
使用Outlook中的VBA代碼根據生日自動向聯絡人發送祝福訊息
若要自動向生日為今天的聯絡人發送祝福訊息,首先需要插入一段VBA代碼,然後您需要創建一個定期任務來觸發該代碼。
以下步驟可能會對您有所幫助:
1. 啟動Outlook,然後按住ALT + F11鍵以打開Microsoft Visual Basic for Applications窗口。
2. 在 Microsoft Visual Basic for Applications窗口中,雙擊Project1(VbaProject.OTM)窗格中的ThisOutlookSession以打開模塊,然後將以下代碼複製並粘貼到空白模塊中。
VBA代碼:根據生日自動向聯絡人發送祝福訊息:
Private Sub Application_Reminder(ByVal Item As Object)
Dim xTempMail As MailItem
Dim xFilePath As String
Dim xItems As Outlook.Items
Dim xItem As Object
Dim xContactItem As Outlook.ContactItem
Dim xTodayDate As String
Dim xBirthdayDate As String
Dim xGreetingMail As Outlook.MailItem
Dim xWordDoc As Word.Document
Dim xGreetings As String
Dim xBool As Boolean
xFilePath = CreateObject("shell.Application").NameSpace(5).self.Path & "\UserTemplates"
Set xFSO = CreateObject("Scripting.FileSystemObject")
If xFSO.FolderExists(xFilePath) = False Then
MkDir xFilePath
End If
If IsFileExists(xFilePath & "\Birthday Greeting Mail.oft") = False Then
Set xTempMail = Outlook.CreateItem(olMailItem)
xTempMail.SaveAs xFilePath & "\Birthday Greeting Mail.oft", olTemplate
xTempMail.Close olDiscard
End If
If (TypeOf Item Is TaskItem) And (Item.Subject = "Send Birthday Greeting Mail") Then
xGreetings = "Happy Birthday!"
xGreetings = InputBox("Input birthday greetings", "Kutools for Outlook", xGreetings)
xTodayDate = Month(Date) & "-" & Day(Date)
Set xItems = Outlook.Application.Session.GetDefaultFolder(olFolderContacts).Items
For Each xItem In xItems
If Not (TypeOf xItem Is ContactItem) Then Exit Sub
Set xContactItem = xItem
xBirthdayDate = Month(xContactItem.Birthday) & "-" & Day(xContactItem.Birthday)
If xBirthdayDate = xTodayDate Then
Set xGreetingMail = Outlook.Application.CreateItemFromTemplate(xFilePath & "\Birthday Greeting Mail.oft")
Set xWordDoc = xGreetingMail.GetInspector.WordEditor
xWordDoc.Range.InsertBefore "Dear " & xContactItem.LastName & Chr(10) & xGreetings & Chr(10) & Chr(10)
With xGreetingMail
.Recipients.Add (xContactItem.Email1Address)
.Subject = "Happy Birthday!"
.Display
.Close (olSave)
.Send
End With
End If
Next
End If
End Sub
Function IsFileExists(ByVal FileName As String) As Boolean
Dim xFileSystem As Object
Set xFileSystem = CreateObject("Scripting.FileSystemObject")
If xFileSystem.FileExists(FileName) = True Then
IsFileExists = True
Else
IsFileExists = False
End If
End Function 
3. 然後點擊Microsoft Visual Basic for Applications窗口中的「工具」 > 「引用」,在彈出的References-Project1對話框中,從可用引用列表框中勾選Microsoft Word Object Library和Microsoft Scripting Runtime選項,參見截圖:

4. 然後點擊「確定」關閉對話框,現在,您應該創建一個任務來觸發VBA代碼。請轉到任務窗格,點擊「新任務」來創建一個任務:
(1.) 在主題行中,您應輸入主題為發送生日祝福郵件;
(2.) 然後點擊任務標籤下的「重複週期」;
(3.) 在任務重複週期對話框中,選擇「按天」並從定期模式部分指定每1天選項;

5. 然後點擊「 確定」關閉對話框,返回任務窗口,請按照以下截圖所示設置定期任務的提醒:

6. 從現在開始,當提醒彈出時,宏將立即被觸發。將會彈出一個對話框提醒您插入生日祝福語,如下截圖所示:

7. 然後點擊「確定」按鈕,祝福郵件將自動發送給生日為今天的聯絡人。
最佳 Office 生產力工具
最新消息:Kutools for Outlook 推出免費版本!
體驗全新 Kutools for Outlook,擁有100+ 強大功能!立即下載!
🤖 Kutools AI :運用先進 AI 技術,輕鬆處理郵件,包括答覆、摘要、優化、擴寫、翻譯與撰寫郵件。
📧 郵件自動化:自動回覆(支援 POP 和 IMAP) / 計劃發送郵件 / 發送郵件時根據規則自動抄送密送 / 自動轉發(高級規則) / 自動新增問候語 / 自動將多收件人郵件分割為個別郵件 ...
📨 郵件管理:撤回郵件 /依主題等條件阻擋詐騙郵件 / 刪除重複郵件 / 高級搜索 / 整合文件夾 ...
📁 附件專業版:批次保存 / 批次拆離 / 批次壓縮 / 自動保存 / 自動拆離 / 自動壓縮 ...
🌟 介面魔法:😊更多精美酷炫表情符號 /重要郵件來臨提醒 / 最小化 Outlook 而非關閉 ...
👍 一鍵神技:帶附件全部答復 /反釣魚郵件 / 🕘顯示發件人時區 ...
👩🏼🤝👩🏻 聯絡人與日曆:批次從選中郵件新增聯絡人 / 將聯絡人組分割為多個組 / 移除生日提醒 ...
以您偏好的語言使用 Kutools —— 支援英語、西班牙語、德語、法語、中文及40 多種其他語言!
只需一鍵即可立即啟用 Kutools for Outlook。立即下載,提升您的效率!