跳至主要内容

如何在Outlook中将所有收件人姓名插入到邮件正文中?

Author: Xiaoyang Last Modified: 2025-05-07

有时,在Outlook中撰写电子邮件时,您可能需要将所有收件人的姓名插入到邮件正文中。当然,您可以逐个将姓名复制并粘贴到正文中。但是,如果有几十个收件人姓名需要粘贴,这将非常繁琐。本文将介绍一种简单的方法来解决这个问题。

使用VBA代码在Outlook中将所有收件人姓名插入到邮件正文


使用VBA代码在Outlook中将所有收件人姓名插入到邮件正文

以下VBA代码可以帮助您将“收件人”字段中的所有姓名插入到邮件正文中,请按照以下步骤操作:

1. 启动Outlook,然后按住 ALT + F11 键打开 Microsoft Visual Basic for Applications 窗口。

2. 单击 插入 > 模块,并将以下代码粘贴到模块窗口中。

VBA代码:将收件人姓名插入邮件正文:

Sub InsertRecipientNamesToBody()
Dim xMailItem As Outlook.MailItem
Dim xRecipient As Outlook.Recipient
Dim xRecipAddress, xRecipNames, xRecipName, xFilterAddr As String
Dim xItems As Outlook.Items
Dim i As Integer
Dim xFoundContact As Outlook.ContactItem
Dim xDoc As Word.Document
On Error Resume Next
Set xMailItem = Outlook.ActiveInspector.CurrentItem
xMailItem.Recipients.ResolveAll
For Each xRecipient In xMailItem.Recipients
    xRecipAddress = xRecipient.Address
    Set xItems = Application.Session.GetDefaultFolder(olFolderContacts).Items
    For i = 1 To 3
        xFilterAddr = "[Email" & i & "Address] = " & xRecipAddress
        Set xFoundContact = xItems.Find(xFilterAddr)
        If Not (xFoundContact Is Nothing) Then
           xRecipNames = xRecipNames & xFoundContact.FullName & Chr(10)
           Exit For
        End If
    Next
    If (xFoundContact Is Nothing) Then
       xRecipName = Split(xRecipAddress, "@")(0)
       xRecipNames = xRecipNames & xRecipName & Chr(10)
    End If
Next
Set xDoc = xMailItem.GetInspector.WordEditor
xDoc.Content.InsertAfter xRecipNames
Set xMailItem = Nothing
Set xRecipient = Nothing
Set xItems = Nothing
Set xFoundContact = Nothing
End Sub

3. 接下来仍然在 Microsoft Visual Basic for Applications 窗口中,单击 工具 > 引用 以打开“引用 - Project1”对话框,并从“可用的引用”列表框中勾选 Microsoft Word Object Library 选项,见截图:

the screenshot of step about inserting all recipient names to email body in outlook 1

4. 然后单击 确定 关闭对话框,现在,您应该将此代码添加到快速访问工具栏中。

5. 单击 新建邮件 创建新邮件,在邮件窗口中,点击 自定义快速访问工具栏 图标,并选择 更多命令,见截图:

the screenshot of step about inserting all recipient names to email body in outlook 2

6. 在 Outlook 选项 对话框中:

(1.) 从“选择命令来自”下拉列表中选择

(2.) 然后选择刚刚插入的VBA代码名称;

(3.) 单击 添加 按钮将代码添加到“自定义快速访问工具栏”列表框中。

the screenshot of step about inserting all recipient names to email body in outlook 3

7. 接下来,一个宏图标会显示在快速访问工具栏中,如下图所示:

the screenshot of step about inserting all recipient names to email body in outlook 4

8. 现在,当您单击该宏图标时,“收件人”字段中的姓名将被插入到邮件正文中,如下图所示:

the screenshot of step about inserting all recipient names to email body in outlook 5