我想从 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>●</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>●</span> Reason e.g. sick, vacation, jury duty<br /><br />" & _
"Thank you!"
另外,您预计在日常使用中会出现什么样的错误?通常,使用不合格的 On Error Resume Next
并不是一个好主意,除非您知道只会收到一个可以忽略的错误。
这是渲染文本的屏幕截图:
关于html - 在电子邮件的 HTML 文本中插入当前日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66802279/