Skip to main content

如何根據Outlook中的收件人自動更改簽名?

Author: Siluvia Last Modified: 2025-05-12

預設情況下,Outlook有一個內建功能,讓使用者在通過不同的電子郵件帳戶發送郵件時自動更改簽名。但除此之外,我將在這裡向您展示如何根據Outlook中「收件人」欄位的不同收件人自動更改簽名的方法。

使用VBA代碼根據收件人自動更改簽名


使用VBA代碼根據收件人自動更改簽名

請按照以下步驟,在Outlook中發送郵件時為相應的收件人應用不同的簽名。

1. 首先,您需要關閉Outlook中的自動附加簽名功能。請點擊從文件導入 > 選項以打開Outlook選項窗口。

2. 在Outlook選項窗口中,選擇左側窗格中的郵件,然後點擊撰寫郵件部分中的簽名按鈕。參見截圖:

using vba to change signature based on recipients automatically with code

3. 在簽名和信紙對話框中,轉到電子郵件簽名標籤下的選擇默認簽名部分,從電子郵件帳戶下拉列表中選擇一個電子郵件帳戶,然後從新郵件回覆/轉寄下拉列表中選擇(無)。重複這些步驟,直到所有電子郵件帳戶都設置為(無)。然後點擊確定按鈕。

using vba to change signature based on recipients automatically with VBA code

注意:您也可以在此簽名和信紙對話框中創建所需的簽名。

4. 當返回Outlook選項窗口時,點擊確定按鈕。

5. 按Alt + F11鍵打開 Microsoft Visual Basic for Applications窗口。

6. 在 Microsoft Visual Basic for Applications窗口中,雙擊左側窗格中的ThisOutlookSession以打開代碼窗口,並將以下VBA代碼複製到該窗口中。參見截圖:

using vba to change signature based on recipients automatically with VBA code

VBA代碼:根據Outlook中的收件人自動更改簽名

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
'Updated by ExtendOffice 2022/08/01
Dim xMailItem As MailItem
Dim xRecipients As Recipients
Dim xRecipient As Recipient
Dim xRcpAddress As String
Dim xSignatureFile, xSignaturePath As String
Dim xFSO As Scripting.FileSystemObject
Dim xDoc As Document
Dim xFindStr As String
On Error Resume Next
Set xFSO = New Scripting.FileSystemObject
If Item.Class <> olMail Then Exit Sub
Set xMailItem = Item
Set xRecipients = xMailItem.Recipients
xSignaturePath = CreateObject("WScript.Shell").SpecialFolders(5) + "\Microsoft\Signatures\"
For Each xRecipient In xRecipients
    If xRecipient.AddressEntry.AddressEntryUserType = olExchangeUserAddressEntry Then
        xRcpAddress = xRecipient.AddressEntry.GetExchangeUser.PrimarySmtpAddress
    Else
        xRcpAddress = xRecipient.AddressEntry.Address
    End If
    Select Case xRcpAddress
        Case "Email Address 1"
            xSignatureFile = xSignaturePath & "aaa.htm"
            Exit For
        Case "Email Address 2", "Email Address 3"
            xSignatureFile = xSignaturePath & "bbb.htm"
            Exit For
        Case "Email Address 4"
            xSignatureFile = xSignaturePath & "ccc.htm"
            Exit For
    End Select
Next
VBA.DoEvents
Set xDoc = xMailItem.GetInspector.WordEditor
xFindStr = "From: " & xMailItem.Recipients.Item(1).Name & " <" & xRcpAddress & ">"
If VBA.InStr(1, xMailItem.Body, xFindStr) <> 0 Then
    xDoc.Application.Selection.HomeKey Unit:=wdStory, Extend:=wdMove
    With xDoc.Application.Selection.Find
        .ClearFormatting
        .Text = xFindStr
        .Execute Forward:=True
    End With
    With xDoc.Application.Selection
        .MoveLeft wdCharacter, 2
        .InsertParagraphAfter
        .MoveDown Unit:=wdLine, Count:=1
    End With
Else
    With xDoc.Application.Selection
        .EndKey Unit:=wdStory, Extend:=wdMove
        .InsertParagraphAfter
        .MoveDown Unit:=wdLine, Count:=1
    End With
End If
xDoc.Application.Selection.InsertFile FileName:=xSignatureFile, Link:=False, Attachment:=False
End Sub

注意:

  • 1). 在VBA代碼中,請將“Email Address 1/2/3/4”替換為特定收件人的電子郵件地址。
  • 2). “aaa.htm”、“bbb.htm”和“ccc.htm”是您將發送給相應收件人的指定簽名。
  • 3). 在這種情況下,簽名“aaa”將發送給“Email Address 1”,簽名“bbb”將發送給“Email Address 2”和“Email Address 3,而“Email Address 4”將收到嵌有簽名“ccc”的郵件。請根據您的需求進行更改。
  • 4). 如果郵件中有多个收件人,代碼只會考慮第一個收件人。在這種情況下,其他收件人將收到與第一個收件人相同簽名的郵件。

7. 然後點擊工具 > 引用以進入引用-項目對話框。在對話框中,請勾選Microsoft Word Object Library Microsoft Scripting Runtime選項,然後點擊確定按鈕,參見截圖:

using vba to change signature based on recipients automatically with VBA code

8. 按Alt + Q鍵關閉Microsoft Visual Basic for Applications窗口。

從現在開始,在編輯完郵件並點擊發送按鈕後,將根據「收件人」欄位中的電子郵件地址自動在郵件正文末尾插入相應的簽名。


在Outlook中發送郵件時自動插入當前日期作為簽名:

如果您希望在Outlook中創建/回覆/轉寄新郵件時將時間戳插入郵件正文作為簽名,您可以啟用Kutools for Outlook的「新建、回覆和轉寄郵件時添加日期簽名 」選項來實現。參見截圖:
立即下載並試用 (30-天免費試用)

using vba to change signature based on recipients automatically with VBA code