html - 在电子邮件的 HTML 文本中插入当前日期

标签 html excel vba outlook

我想从 Excel 发送 Outlook 电子邮件。

我需要在 HTML 代码中插入今天的日期。

尝试设置日期格式时,电子邮件正文中的

“strDate”不起作用。

我尝试了替换功能、格式(日期)等方式。

Sub SharePerformance1()
    Dim xOutApp As Object
    Dim xOutMail As Object
    Dim xOutMsg As String
    Dim strDate As String

    On Error Resume Next

    Set xOutApp = CreateObject("Outlook.Application")
    Set xOutMail = xOutApp.CreateItem(0)
    strDate = Format(Date, "dd mmm yyyy")
    xOutMsg = "Good morning!<br />This pay period is from <b><span style=""color:#CE0426"">&strDate&</span style=""color:#CE0426""></b>To help ensure you are paid accurately and timely, please follow the instructions below.<br /><br />" & _
              "<u>Salaried team members</u><br />" & _
              "<span style=font-size:5px>&#9679;</span> Reason e.g. sick, vacation, jury duty<br /><br />" & _
              "Thank you!"
                  
    With xOutMail
        .To = ""
        .CC = ""
        .BCC = ""
        .Subject = "Notice" & Format(Now() + 1, "dddd, mmmm dd") & " for Pay Period " & Format(Now() - 8, "dd") & "-" & Format(Now() + 3, "dd/yyyy")
        .HTMLBody = xOutMsg
        .Display
    End With
    Set xOutMail = Nothing
    Set xOutApp = Nothing
End Sub

最佳答案

这在这里工作正常:(注意:根据 @NickAbbot 进行编辑以删除多余的&符号)

xOutMsg = "Good morning!<br />This pay period is from <b><span style=""color:#CE0426"">" & strDate & ". </span style=""color:#CE0426""></b>To help ensure you are paid accurately and timely, please follow the instructions below.<br /><br />" & _
              "<u>Salaried team members</u><br />" & _
              "<span style=font-size:5px>&#9679;</span> Reason e.g. sick, vacation, jury duty<br /><br />" & _
              "Thank you!"

另外,您预计在日常使用中会出现什么样的错误?通常,使用不合格的 On Error Resume Next 并不是一个好主意,除非您知道只会收到一个可以忽略的错误。

这是渲染文本的屏幕截图:

enter image description here

关于html - 在电子邮件的 HTML 文本中插入当前日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66802279/

相关文章:

css - 哪些浏览器支持-webkit-text-fill-color?

excel - XLL 和 vba 函数名称冲突导致永久#NAME?错误

vba - MS Access VBA 循环遍历列表

excel - SSRS 2008 R2 - 导出到 Excel 时的 MAX TABS

excel - 我的代码出现 "Permission Denied"错误

arrays - VBA从多维数组中获取最小值/最大值

javascript - 将返回的 URL 加粗

javascript - JQuery .autocomplete 和 html

html - 在具有动态宽度的内联元素旁边 float 元素

vba - 为什么可以使用附加参数调用返回Variant的函数?