簡體   English   中英

Excel VBA中的Outlook電子郵件和簽名 - .Body vs .HTMLbody

[英]Outlook Email and Signature from Excel VBA - .Body vs .HTMLbody

我的工作表上有一個按鈕來發送電子郵件(加上更多,但不重要)。 我想要我的默認簽名與HTML格式,但兩個選項都沒有產生我想要的結果:

  • .Body生成正確的正文(字體和回車),但簽名是純文本

  • .HMTLBody產生正確的簽名但由於某種原因身體,字體轉到Times New Roman而不是默認的Calibri,並且無論我使用vbNewLinevbCr還是vbCrLf ,回車都不起作用

我只是SOL嗎? 我需要選擇一個並處理它,還是有辦法讓我吃蛋糕也吃它?

碼:

    .Display         ' need to display email first for signature to work
    .Subject = Title
    .To = ActiveSheet.Range("E10").Value ' <-- Put email of the recipient here
    .CC = "" ' <-- Put email of 'copy to' recipient here
    .HTMLBody = "Thank you for the opportunity to bid on " & ActiveSheet.Range("B9").Value & ". " & _
        " Please read our attached proposal in its entirety to be sure of all inclusions, exclusions, and products proposed.  Give us a call with any questions or concerns." & _
        vbCrLf & vbCrLf & _
        "Thank you," & _
        .HTMLBody      ' Adds default signature
    .Attachments.Add PdfFile

更新:

最后的工作代碼歸功於以下兩個答案的幫助:

.Display         ' We need to display email first for signature to be added
.Subject = Title
.To = ActiveSheet.Range("E10").Value
.CC = ""
.HTMLBody = "<font face=""calibri"" style=""font-size:11pt;"">Thank you for the opportunity to bid on " & ActiveSheet.Range("B9").Value & ". " & " Please read our attached proposal in its entirety to be sure of all inclusions, exclusions, and products proposed.  Give us a call with any questions or concerns." & _
    "<br><br>" & _
    "Thank you," & _
    .HTMLBody & "</font>"   ' Adds default signature
.Attachments.Add PdfFile

設置HTMLBody屬性時,請確保合並現有的HTMLBody (帶簽名)和新數據 - 您不能只連接兩個HTML字符串並期望有效的HTML。 找到"<body"字符串的位置,找到下一個“>”的位置(用屬性處理body元素),在“>”之后插入數據。

嘗試將您的數據插入正確的html標記:

.HTMLBody = "<font face=""verdana"" color=""black"">This is some text!</font>"

對於空格,您必須添加此標記"<br>" ,例如:

.HTMLBody = "<font face=""calibri"" color=""black""> hello <br>"
.HTMLBody = .HTMLBody & " how <br>" & " are <br>" & " you?</font>"

造成:

你好


怎么樣



您?

EDIT2

為了插入圖像(簽名為圖像),您可以使用以下代碼:

一步。 將此代碼復制到類模塊中,並將該類模塊命名為“MailOptions”

Private Message As CDO.Message
Private Attachment, Expression, Matches, FilenameMatch, i

Public Sub PrepareMessageWithEmbeddedImages(ByVal FromAddress, ByVal ToAddress, ByVal Subject, ByVal HtmlContent)

    Set Expression = CreateObject("VBScript.RegExp")
    Expression.Pattern = "\<EMBEDDEDIMAGE\:(.+?)\>"
    Expression.IgnoreCase = True
    Expression.Global = False 'one match at a time

    Set Message = New CDO.Message
    Message.From = FromAddress
    Message.To = ToAddress
    Message.Subject = Subject

    'Find matches in email body, incrementally increasing the auto-assigned attachment identifiers
    i = 1
    While Expression.Test(HtmlContent)
        FilenameMatch = Expression.Execute(HtmlContent).Item(0).SubMatches(0)
        Set Attachment = Message.AddAttachment(FilenameMatch)
        Attachment.Fields.Item("urn:schemas:mailheader:Content-ID") = "<attachedimage" & i & ">" ' set an ID we can refer to in HTML
        Attachment.Fields.Item("urn:schemas:mailheader:Content-Disposition") = "inline" ' "hide" the attachment
        Attachment.Fields.Update
        HtmlContent = Expression.Replace(HtmlContent, "cid:attachedimage" & i) ' update the HTML to refer to the actual attachment
        i = i + 1
    Wend

    Message.HTMLBody = HtmlContent
End Sub

Public Sub SendMessageBySMTP(ByVal SmtpServer, ByVal SmtpUsername, ByVal SmtpPassword, ByVal UseSSL)
    Dim Configuration
    Set Configuration = CreateObject("CDO.Configuration")
    Configuration.Load -1 ' CDO Source Defaults
    Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SmtpServer
    'Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = SmtpPort
    Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 30

    If SmtpUsername <> "" Then
        Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
        Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = SmtpUsername
        Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = SmtpPassword
    End If
    Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = UseSSL
    Configuration.Fields.Update
    Set Message.Configuration = Configuration
    Message.Send
End Sub

步驟2.在標准模塊中,您將詳細說明.html內容並從類中實例化一個對象:

public sub send_mail()

Dim signature As String
dim mail_sender as new MailOptions 'here you are instantiating an object from the class module created previously
dim content as string

signature = "C:\Users\your_user\Documents\your_signature.png"

content = "<font face=""verdana"" color=""black"">This is some text!</font>"
content = content & "<img src=""<EMBEDDEDIMAGE:" & signature & " >"" />"

mail_sender.PrepareMessageWithEmbeddedImages _
                    FromAddress:="chrism_mail@blablabla.com", _
                    ToAddress:="addressee_mail@blablabla.com", _
                    Subject:="your_subject", _
                    HtmlContent:=content

'your_Smtp_Server, for example: RelayServer.Contoso.com
correos.SendMessageBySMTP "your_Smtp_Server", "your_network_user_account", "your_network_user_account_password", False

end sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM