如何在电子邮件正文中包含发件人的详细信息?

时间:2020-06-03 19:33:59

标签: vba outlook ms-word

我在MS Word中有一个宏:

  • 单击按钮即可创建Outlook电子邮件
  • 将Word文档附加到Outlook电子邮件中

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

    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

2 个答案:

答案 0 :(得分:0)

您可以使用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属性。

答案 1 :(得分:0)

最终使用WScript.Network

Function GetUserFullName() As String
    Dim WSHnet, UserName, UserDomain, objUser
    Set WSHnet = CreateObject("WScript.Network")
    UserName = WSHnet.UserName
    UserDomain = WSHnet.UserDomain
    Set objUser = GetObject("WinNT://" & UserDomain & "/" & UserName & ",user")
    GetUserFullName = objUser.FullName
End Function

我还使用 .HTMLbody 作为电子邮件正文[我在其中调用GetUserFullName()]

,而不是使用 .body
.HTMLbody = "Request submitted by:" & "<br>" & GetUserFullName()