Skip to main content

Kutools for Office — 一套工具,五種功能。完成更多工作。

如何在Outlook中自動向生日為今天的聯絡人發送祝福訊息?

Author Xiaoyang Last modified

有時候,您可能希望在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 
the screenshot of step about using vba to send a greeting message to a contact automatically if his birthday is today in Outlook 1

3. 然後點擊Microsoft Visual Basic for Applications窗口中的「工具」 > 「引用」,在彈出的References-Project1對話框中,從可用引用列表框中勾選Microsoft Word Object LibraryMicrosoft Scripting Runtime選項,參見截圖:

4. 然後點擊「確定」關閉對話框,現在,您應該創建一個任務來觸發VBA代碼。請轉到任務窗格,點擊「新任務」來創建一個任務:

(1.) 在主題行中,您應輸入主題為發送生日祝福郵件

(2.) 然後點擊任務標籤下的「重複週期」;

(3.) 在任務重複週期對話框中,選擇「按天」並從定期模式部分指定每1天選項;

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

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

7. 然後點擊「確定」按鈕,祝福郵件將自動發送給生日為今天的聯絡人。


最佳辦公室生產力工具

最新消息:Kutools for Outlook 推出免費版本!

體驗全新 Kutools for Outlook,超過100項精彩功能!立即下載!

🤖 Kutools AI 採用先進的AI技術輕鬆處理郵件,包括答覆、摘要、優化、擴充、翻譯及撰寫郵件。

📧 郵件自動化自動回覆(支援POP及IMAP) / 排程發送郵件 / 發送郵件時根據規則自動抄送密送 / 自動轉發(高級規則) / 自動添加問候語 / 自動分割多收件人郵件為個別郵件 ...

📨 郵件管理撤回郵件 / 根據主題等方式阻止詐騙郵件 / 刪除重複郵件 / 高級搜索 / 整合文件夾 ...

📁 附件專業工具批量保存 / 批量拆離 / 批量壓縮 / 自動保存 / 自動拆離 / 自動壓縮 ...

🌟 介面魔法😊更多精美與酷炫表情符號 /重要郵件來臨時提醒 / 最小化 Outlook 而非關閉 ...

👍 一鍵便利帶附件全部答復 / 防詐騙郵件 / 🕘顯示發件人時區 ...

👩🏼‍🤝‍👩🏻 聯絡人與日曆從選中郵件批量添加聯絡人 / 分割聯絡人組為個別組 / 移除生日提醒 ...

以您偏好的語言使用 Kutools,支援英語、西班牙語、德語、法語、中文及超過40種其他語言!

只需點擊一次,即可立即解鎖 Kutools for Outlook。別等了,現在下載提升您的工作效率!

kutools for outlook features1 kutools for outlook features2

🚀 一鍵下載 — 獲取全部 Office 插件

強力推薦:Kutools for Office(5合1)

一鍵下載五個安裝程式,包括 Kutools for Excel, Outlook, Word, PowerPointOffice Tab Pro 立即下載!

  • 一鍵便利:一次操作即可下載全部五套安裝包。
  • 🚀 隨時處理任何 Office 任務:安裝您需求的插件,隨時隨地。
  • 🧰 包含:Kutools for Excel / Kutools for Outlook / Kutools for Word / Office Tab Pro / Kutools for PowerPoint