在电子邮件的 HTML 文本中插入当前日期
Insert current date inside HTML text for emails
我想从 excel 发送 outlook 电子邮件。
我需要在 HTML 代码中插入今天的日期。
"strDate" 在电子邮件正文中尝试格式化日期时不起作用。
我尝试了replace function, Format (Date)等方式
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
变量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
两个变化:
您需要在“通知”后加上 space
.Subject = "通知 " & 格式...
不是这个&strDate&
这:“& strDate &”
这在这里工作正常:(注意:根据@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
通常不是一个好主意,除非你知道你只会得到一个可以忽略的错误。
这是呈现文本的屏幕截图:
我想从 excel 发送 outlook 电子邮件。
我需要在 HTML 代码中插入今天的日期。
"strDate" 在电子邮件正文中尝试格式化日期时不起作用。
我尝试了replace function, Format (Date)等方式
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
变量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
两个变化:
您需要在“通知”后加上 space .Subject = "通知 " & 格式...
不是这个&strDate& 这:“& strDate &”
这在这里工作正常:(注意:根据@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
通常不是一个好主意,除非你知道你只会得到一个可以忽略的错误。
这是呈现文本的屏幕截图: