【问题标题】:Insert current date inside HTML text for emails在电子邮件的 HTML 文本中插入当前日期
【发布时间】:2021-06-22 10:00:31
【问题描述】:

我想从 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

【问题讨论】:

  • 不起作用是什么意思?其他一切正常吗?
  • "strDate" 应显示为当前日期,例如“03/25/2021”。但是当我尝试运行代码时,它反而显示了“strDate”这个词。如果我不使用 HTML 着色或仅运行它,它可能会显示“03/25/2021”。
  • 你有 strDate 内引号而不是外引号。可能已发布的解决方案之一将引导您朝着正确的方向前进。

标签: html excel vba outlook


【解决方案1】:

这在这里工作得很好:(注意:根据@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 通常不是一个好主意,除非您知道只会收到一个可以忽略的错误。

这是渲染文本的截图:

【讨论】:

  • @NickAbbot 谢谢。自从我用 HTML 编写代码以来已经有很长时间了。我删除了它们并在日期之后添加了一个句号。
  • 非常感谢您的回答!现在可以了。不敢相信 !我花了几天的时间,但你们只需要几分钟就可以找到解决方案!
【解决方案2】:

变量strDate 需要在引号之外。我已经在下面的代码中修复了这个问题,并且还整理了一些东西。特别是,我将 HTML 的创建分成了多行,希望它更易于维护、调试和添加。

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

    Set xOutApp = CreateObject("Outlook.Application")
    Set xOutMail = xOutApp.CreateItem(0)
    
    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!"
                  
    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

【讨论】:

  • 如果我把 strDate 放在引号旁边,它只显示单词“strDate”,而不是日期:(
【解决方案3】:

两个变化:

  1. “通知”后面需要一个空格 .Subject = "通知" & 格式...

  2. 不是这个&strDate& 这个:" & strDate & "

【讨论】:

  • 这个方法我试过了,也没用!
  • 你需要两个引号: ...426"">" & strDate & "
猜你喜欢
  • 2016-07-25
  • 1970-01-01
  • 2014-05-28
  • 1970-01-01
  • 1970-01-01
  • 2016-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多