首页 > 解决方案 > 如何在电子邮件正文中包含发件人的详细信息?

问题描述

我在 MS Word 中有一个宏:

我想在电子邮件正文中包含发件人的详细信息(姓名、电子邮件地址等)。

    Dim Outlook_Object As Object
    
    Dim Email_Object As Object
    Dim This_document As Document
    Application.ScreenUpdating = False

    
    Set Outlook_Object = CreateObject("Outlook.Application")
    Set Email_Object = Outlook_Object.CreateItem(olMailItem)
    Set This_document = ActiveDocument
    
    This_document.Save
    With Email_Object
        .Subject = "REPORT REQUEST FORM"
        .Body = "This is a test email."
        .To = "john.smith@gmail.com"
        .Importance = olImportanceNormal
        .Attachments.Add This_document.FullName
        .Display
   
    End With
    Set This_document = Nothing
    Set Email_Object = Nothing
    Set Outlook_Object = Nothing
    Application.ScreenUpdating = True

标签: vbaoutlookms-word

解决方案


您可以使用NameSpace.CurrentUser属性,该属性将当前登录用户的显示名称作为Recipient对象返回。Recipient.AddressEntry属性返回AddressEntry对应于已解析收件人的对象。然后你可以得到NameAddress属性,如下所示:

Dim ns as Outlook.NameSpace

Set ns = OutlookApplication.GetNamespace("MAPI")

' to get the email address of the sender
ns.CurrentUser.AddressEntry.Address

' to get the name
ns.CurrentUser.AddressEntry.Name

要将此信息包含到消息正文中,您可以使用BodyHTMLBody属性。


推荐阅读