【问题标题】:Issue with styles in HTMLbody in VBAVBA中HTMLbody中的样式问题
【发布时间】:2021-04-28 20:48:00
【问题描述】:

我在使用 VBA 设置电子邮件正文时遇到问题。我需要使用不同的字体大小和颜色自定义电子邮件正文。因此我使用了 .HTMLBody 函数。 有一次,我已经执行了它工作正常的代码,但在 Outlook 显示中没有给出任何格式。 如果有人能就这个问题提供帮助,非常感谢。

VBA 代码:

 .HTMLBody = "Hi <strong>All</strong>" & "<br>" & "<br>" & _
                "Your task has already overdue! Please focus your kind attention here." & "<br>" & _
                "<br>" & _
                "<strong>" & "<span style=" & "color: rgb(41, 105, 176);" & ">" & "Task &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;" & "</span>" & "</strong>:" & "<span style=" & "color: rgb(184, 49, 47);" & ">" & "testing task testing task" & "</span> " & "<br>" & _
                "<span style=" & "color: rgb(41, 105, 176);" & ">" & "<strong>" & "Decision" & "</strong>" & "</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:" & "<span style=" & "color: rgb(184, 49, 47);" & ">" & "Test action test action" & "</span>" & "<br>" & _
                "<span style=" & "color: rgb(41, 105, 176);" & ">" & "<strong>Due Date&nbsp;" & "</strong>" & "</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:" & "<span style=" & "color: rgb(184, 49, 47);" & ">" & "21/1/2021" & "</span>" & "<br>" & _
                "<br>" & "<br>" & "<br>" & _
                "<span style=" & "font-size: 12px;" & ">" & "This is system generated email from Measure and Monitoring team." & "</span>" & "<br>" & _
                "<span style=" & "font-size: 12px;" & ">" & "If you already completed or any convenience regarding this e mail please reply here." & "</span>" & "<br>" & _
                "<span style=" & "font-size: 10px;" & ">" & "Generated Date : 1/1/2021" & "</span>" & _
                "<br>"

【问题讨论】:

  • 我已经执行了代码,它可以正常工作,但在 Outlook 显示中没有给出任何格式 这是什么意思?文本格式为粗体,因此它显然有效
  • 嗨。不,我需要为这些词添加颜色。我使用 "

标签: html excel vba format


【解决方案1】:

那是因为你错了一半。如果它不起作用,那么看看 VBA 将如何格式化它。一个错误: style=

后没有引号

自己测试一下:

Option Explicit
Sub test()
Dim HTMLBody As String

HTMLBody = "Hi <strong>All</strong>" & "<br>" & "<br>" & _
                "Your task has already overdue! Please focus your kind attention here." & "<br>" & _
                "<br>" & _
                "<strong>" & "<span style=" & "color: rgb(41, 105, 176);" & ">" & "Task &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;" & "</span>" & "</strong>:" & "<span style=" & "color: rgb(184, 49, 47);" & ">" & "testing task testing task" & "</span> " & "<br>" & _
                "<span style=" & "color: rgb(41, 105, 176);" & ">" & "<strong>" & "Decision" & "</strong>" & "</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:" & "<span style=" & "color: rgb(184, 49, 47);" & ">" & "Test action test action" & "</span>" & "<br>" & _
                "<span style=" & "color: rgb(41, 105, 176);" & ">" & "<strong>Due Date&nbsp;" & "</strong>" & "</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:" & "<span style=" & "color: rgb(184, 49, 47);" & ">" & "21/1/2021" & "</span>" & "<br>" & _
                "<br>" & "<br>" & "<br>" & _
                "<span style=" & "font-size: 12px;" & ">" & "This is system generated email from Measure and Monitoring team." & "</span>" & "<br>" & _
                "<span style=" & "font-size: 12px;" & ">" & "If you already completed or any convenience regarding this e mail please reply here." & "</span>" & "<br>" & _
                "<span style=" & "font-size: 10px;" & ">" & "Generated Date : 1/1/2021" & "</span>" & _
                "<br>" & vbCrLf
Debug.Print HTMLBody

End Sub

【讨论】:

  • 嗨,如果我在 style= 之后删除了引号,它会给我一个错误并且代码不起作用。
  • 你能告诉我们你遇到了什么错误吗?
  • 错误意味着如果我删除引号,并编写如下代码,"" & "" & it没有给出任何颜色。它只加粗文本。与我在图中显示的结果相同。
  • 您不能删除引号,这不是有效的 CSS。请告诉我们您在使用引号时遇到了什么错误。
猜你喜欢
  • 2013-04-15
  • 1970-01-01
  • 2013-09-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-11
  • 2012-04-21
  • 2011-02-27
  • 1970-01-01
相关资源
最近更新 更多