【问题标题】:vba outlook signature with sender name带有发件人姓名的 vba Outlook 签名
【发布时间】:2017-08-04 23:55:12
【问题描述】:

我搜索了很多问题,但找不到与我想做的事情相匹配的东西。

我有这个 Outlook 代码可以通过电子邮件发送我的名为 Pedidos 的表格。

Sub Mail_ActiveSheet()

    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim Sourcewb As Workbook
    Dim Destwb As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim OutApp As Object
    Dim OutMail As Object
    Dim sCC As String
    Dim Signature As String

    sCC = Range("copia").Value
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    Set Sourcewb = ActiveWorkbook

    Sheets("Pedidos").Copy
    Set Destwb = ActiveWorkbook

    ' Determine the Excel version, and file extension and format.
    With Destwb
        If Val(Application.Version) < 12 Then
            ' For Excel 2000-2003
            FileExtStr = ".xls": FileFormatNum = -4143
        Else
            ' For Excel 2007-2010, exit the subroutine if you answer
            ' NO in the security dialog that is displayed when you copy
            ' a sheet from an .xlsm file with macros disabled.
            If Sourcewb.Name = .Name Then
                With Application
                    .ScreenUpdating = True
                    .EnableEvents = True
                End With
                MsgBox "You answered NO in the security dialog."
                Exit Sub
            Else
                Select Case Sourcewb.FileFormat
                Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
                Case 52:
                    If .HasVBProject Then
                        FileExtStr = ".xlsm": FileFormatNum = 52
                    Else
                        FileExtStr = ".xlsx": FileFormatNum = 51
                    End If
                Case 56: FileExtStr = ".xls": FileFormatNum = 56
                Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
                End Select
            End If
        End If
    End With


    '    With Destwb.Sheets(1).UsedRange
    '        .Cells.Copy
    '        .Cells.PasteSpecial xlPasteValues
    '        .Cells(1).Select
    '    End With
    '    Application.CutCopyMode = False

    ' Save the new workbook, mail, and then delete it.
    TempFilePath = Environ$("temp") & "\"
    TempFileName = Sourcewb.Sheets("Consulta").Range("F2:G2").Value & " " _
                 & IIf(Len(Day(Now)) = 1, "0" & Day(Now), Day(Now)) & IIf(Len(Month(Now)) = 1, "0" & Month(Now), Month(Now)) & Year(Now) & Hour(Now) & Minute(Now) & Second(Now)

    Set OutApp = CreateObject("Outlook.Application")

    Set OutMail = OutApp.CreateItem(0)

    With Destwb
        .SaveAs TempFilePath & TempFileName & FileExtStr, _
                FileFormat:=FileFormatNum
        On Error Resume Next
        On Error GoTo 0
       ' Change the mail address and subject in the macro before
       ' running the procedure.
        With OutMail
            .to = "example@example.com"
            .CC = sCC
            .BCC = ""
            .Subject = "[PEDIDOS 019] " & TempFileName
            .HTMLBody = "<font face=""calibri"" color=""black""> Olá Natalia, <br>"
            .HTMLBody = .HTMLBody & " Por favor, fazer a requisição dos pedidos em anexo. <br>" & " Obrigado!<br>" & xxxxx & "</font>"
            .Attachments.Add Destwb.FullName
            ' You can add other files by uncommenting the following statement.
            '.Attachments.Add ("C:\test.txt")
            ' In place of the following statement, you can use ".Display" to
            ' display the mail.
            .SEND
        End With
        On Error GoTo 0
        .Close SaveChanges:=False
    End With

    ' Delete the file after sending.
    Kill TempFilePath & TempFileName & FileExtStr

    Set OutMail = Nothing
    Set OutApp = Nothing

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub

如您所见,下一行中的xxxxx 代表我的签名,我想获取我的电子邮件(在我发送时)并将其写在那里(或姓名和姓氏)。

   .HTMLBody = "<font face=""calibri"" color=""black""> Olá Natalia, <br>"
    .HTMLBody = .HTMLBody & " Por favor, fazer a requisição dos pedidos em anexo. <br>" & " Obrigado!<br>" & xxxxx & "</font>"

所以我真的把这个xxxxx 称为我的电子邮件,或者可能是我的名字,例如。

我已经检查了MailItem.SenderName 属性,但我不明白如何使用它。这是我第一次使用 VBA 发送电子邮件,因此我们将不胜感激。

【问题讨论】:

  • 用户是否会在 Outlook 中设置一个预定义的签名,该签名始终与新邮件一起出现?
  • @ScottHoltzman 不,我想要的唯一签名是发件人的姓名或地址
  • 查看this answer 了解如何获取当前用户的电子邮件或姓名。

标签: vba excel outlook


【解决方案1】:

在发送邮件之前,SenderName 将不可用。

Option Explicit

Sub Signature_Insert()

    Dim OutApp As Object
    Dim OutMail As Object
    Dim nS As Object

    Dim signature As String

    Set OutApp = CreateObject("Outlook.Application")
    Set nS = OutApp.GetNamespace("mapi")

    Debug.Print nS.CurrentUser
    Debug.Print nS.CurrentUser.name ' default property

    Debug.Print nS.CurrentUser.Address
    Debug.Print nS.CurrentUser.AddressEntry.GetExchangeUser.PrimarySmtpAddress

    signature = nS.CurrentUser
    'signature = nS.CurrentUser.Address

    Set OutMail = OutApp.CreateItem(0)

    With OutMail
        .To = "example@example.com"
        .CC = "sCC"
        .BCC = ""
        .Subject = "[PEDIDOS 019] " & "TempFileName"
        .HTMLBody = "<font face=""calibri"" color=""black""> Olá Natalia, <br>"
        .HTMLBody = .HTMLBody & " Por favor, fazer a requisição dos pedidos em anexo. <br>" & " Obrigado!<br>" & signature & "</font>"
        .Display
    End With

ExitRoutine:
    Set OutApp = Nothing
    Set nS = Nothing
    Set OutMail = Nothing

End Sub

【讨论】:

    【解决方案2】:

    试试下面的代码就行了

    .HTMLBody = .HTMLBody & " Por favor, fazer a requisição dos pedidos em anexo. <br>" & " Obrigado!<br>" & .To & "</font>"
    

    只需将 XXXXX 替换为 .To,它将在您的签名中添加“.To

    【讨论】:

    • 对不起,我刚刚意识到我拼错了我的问题。它应该是我自己的电子邮件而不是example@example.com。如果我有一些名为.From 的属性,也许我也可以这样做。
    • 尝试使用 .From 我得到了类型不匹配。
    • 您需要添加代码“.SentOnBehalfOfName = “email@email.com”。您必须手动添加它或从任何 Excel 表中提供参考,例如。“.SentOnBehalfOfName = Sheet1.cells( 1,1)"
    • 我明白了,但是如果不同的人访问该工作表,我将无法插入静态电子邮件为 .SentOnBehalfOfName = "email@email.com...
    猜你喜欢
    • 2022-08-14
    • 1970-01-01
    • 1970-01-01
    • 2018-04-27
    • 1970-01-01
    • 1970-01-01
    • 2020-02-08
    • 2011-05-07
    • 2018-01-20
    相关资源
    最近更新 更多