提问者:小点点

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


我需要一个HTML代码的帮助。我正在用VBA编写一个从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

共2个答案

匿名用户

变量strdate需要在引号之外。

strDate = Format(Date, "dd mmm yyyy")
xOutMsg = xOutMsg & "Good morning!<br><br>"
xOutMsg = xOutMsg & "This pay period is from <b><span style=""color:#CE0426"">" & strDate & "</span></b><br><br>"
xOutMsg = xOutMsg & "To help ensure you are paid accurately and timely, please follow the instructions below.<br><br>"
xOutMsg = xOutMsg & "<u>Salaried team members</u><br>"
xOutMsg = xOutMsg & "<ul><li>Reason e.g. sick, vacation, jury duty</li></ul><br>"
xOutMsg = xOutMsg & "Thank you!"

匿名用户

两个变化:

>

  • 在“notice”后面需要空格。subject=“notice”&格式...

    不是this&strDate&this:“&strDate&”