【问题标题】:Outlook Email and Signature from Excel VBA - .Body vs .HTMLbodyExcel VBA 中的 Outlook 电子邮件和签名 - .Body 与 .HTMLbody
【发布时间】:2016-07-12 16:38:22
【问题描述】:

我的工作表上有一个用于发送电子邮件的按钮(还有更多,但不重要)。我想要我的默认签名及其 HTML 格式,但两个选项都没有产生我想要的结果:

  • .Body 生成正确的正文(字体和回车),但签名是纯文本

  • .HMTLBody 生成正确的签名,但由于某种原因,正文使用 Times New Roman 而不是默认的 Calibri,并且无论我使用 vbNewLine、@987654325,回车都不起作用@,或vbCrLf

我只是 S.O.L. 吗?我只需要挑选一个并处理它,还是有办法让我的蛋糕也吃掉?

代码:

    .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

【问题讨论】:

  • 这是 Outlook 的一个已知问题 - 它只是忽略您的 HTML 字体选择并退回到 TNR。 This thread on Litmus 提供了一些解决方法。

标签: vba excel email outlook signature


【解决方案1】:

当您设置 HTMLBody 属性时,请确保合并现有的 HTMLBody(带有签名)和您的新数据 - 您不能仅仅连接两个 HTML 字符串并期望一个有效的 HTML。 找到"&lt;body" 字符串的位置,找到下一个“>”的位置(以处理带有属性的正文元素),在“>”之后插入您的数据。

【讨论】:

  • 你把我弄丢了。你有 24k 代表所以我假设你知道你在说什么,但我不知道。我实际上并没有在我的签名中使用html 代码,只有一个徽标图像和一些通过 Outlook 的签名对话框格式化的字体大小和颜色。话虽这么说,我确实对 html 了解很多,所以请告诉我是否有办法改变我的 VBA 代码来执行您的建议?您需要查看所有代码吗?
  • 好的,当我看到 Dani 的回答时,我明白了你的意思,但我仍然无法在其中获得我的签名
  • 仍在努力。我想我已经解决了,但现在突然,一夜之间我的 Excel 2016 损坏了!每次我尝试保存任何文件时都会崩溃!所以我现在正在处理这个问题。
  • 上面发布的最新代码。我仍然不能 100% 确定连接 2 个 HTML 字符串是什么意思。我试图连接的 2 个不同的字符串是什么?
  • btw - 看起来我正在处理的那个工作簿已经损坏了。很奇怪。从一个新的工作簿开始,一切都恢复正常了。
【解决方案2】:

尝试将您的数据插入到正确的 html 标签中:

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

空格必须加上这个标签"&lt;br&gt;",例如:

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

结果:

你好


如何



你?

编辑2

为了插入图片(签名为图片),您可以使用以下代码:

1 步。 将此代码复制并粘贴到类模块中,并将该类模块命名为“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

【讨论】:

  • 这确实使字体正确,但是根本没有添加签名。
  • 当我单步执行代码时,签名在第一次显示电子邮件时就在那里,并在填写电子邮件时一直留在那里,直到插入.HTMLBody,然后它就消失了。跨度>
  • 我认为这是在哪里或是否使用&lt;/font&gt; 标签的问题。当我删除它时,我很高兴。
  • 您不能连接 2 个 HTML 字符串 - HTMLBody 返回的值是以 html 标记开头的完全限定的 HTML 字符串 - 如果您添加任何额外的 HTML,它将被添加到 HTML 文档之外。
  • @DaniAya 谢谢所有这些,但图像只是我签名的一部分,而不是签名本身。我现在确实可以使用它,不确定&lt;/font&gt; 标签发生了什么,但它一定与我的工作簿在我昨晚工作时以某种方式损坏的事实有关。做了一个新的,现在一切都很好。我想我会更新我的原始帖子以显示我的工作代码。仍在考虑复选标记,因为你们俩都有些正确。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-21
  • 2013-04-15
  • 2018-05-26
  • 2018-03-25
  • 1970-01-01
  • 2013-10-06
  • 1970-01-01
相关资源
最近更新 更多