繁体   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