跳至主要内容

如何在Outlook中根据收件人自动更改签名?

Author: Siluvia Last Modified: 2025-05-07

默认情况下,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代码中,请将“电子邮件地址1/2/3/4”替换为特定收件人的电子邮件地址。
  • 2). “aaa.htm”、“bbb.htm”和“ccc.htm”是您要发送给相应收件人的指定签名。
  • 3). 在这种情况下,签名“aaa”将发送给“电子邮件地址1”,签名“bbb”将发送给“电子邮件地址2”和“电子邮件地址3,而“电子邮件地址4”将收到嵌入签名“ccc”的电子邮件。请根据您的需求进行更改。
  • 4). 如果一封电子邮件中有多个收件人,代码只考虑第一个收件人。在这种情况下,其他收件人将收到与第一个收件人相同签名的电子邮件。

7. 然后点击 工具 > 引用 进入“引用-项目”对话框。在对话框中,请同时勾选 Microsoft Word 对象库Microsoft 脚本运行时 选项,然后单击 确定 按钮,参见截图:

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