如何从单元格 - VBA 的 Body 电子邮件中获取 Link
How do I get a Link in Body of email from cell - VBA
我在这个特定查询上花了 3 天时间,所以非常感谢任何帮助。
我创建了一个宏,可以在电子表格中的某个日期前 30 天向特定的人发送电子邮件,将文档的标题放在主题中,并标记我的电子表格以标记为电子邮件已发送。工作完美!但是,我想将 link 从电子表格中的 link 放入电子邮件中,这样电子邮件收件人只需单击 link 即可直接转到文档进行审阅。我正在尝试使用 href 和 HTMLBody 函数,但没有成功,只是费了很大力气。如果有人能给我任何帮助,那就太好了。我附上了我正在使用的代码。正如我所说,在我使用 strbody 之前效果很好。我只是无法弄清楚我在这里做错了什么。非常感谢
Sub datesexcelvba()
Dim myApp As Outlook.Application, mymail As Outlook.MailItem
Dim mydate1 As Date
Dim mydate2 As Long
Dim datetoday1 As Date
Dim datetoday2 As Long
Dim strbody As String
Dim x As Long
lastrow = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
For x = 2 To lastrow
mydate1 = Cells(x, 8).Value
mydate2 = mydate1
Cells(x, 11).Value = mydate2
datetoday1 = Date
datetoday2 = datetoday1
Cells(x, 12).Value = datetoday2
If mydate2 - datetoday2 = 30 Then
Set myApp = New Outlook.Application
Set mymail = myApp.CreateItem(olmailitem)
strbody = "Good Morning" & vbCrLf & _
"Please be aware that this file is due for review in the next 30 days" & vbCrLf & _
"Please click "<a href= Cells,(x, 13)" >here</ a>" &vbCrLF & _
"Regards" & vbCrLf & _
"Master Control Document"
mymail.To = Cells(x, 7).Value
With mymail
.Subject = "Review Reminder " & Cells(x, 4).Value
.HTMLBody = .HTMLBody & strbody
.Display
'.send
End With
Set myApp = Nothing
Set mymail = Nothing
Cells(x, 9) = "Yes"
Cells(x, 9).Font.ColorIndex = 3
Cells(x, 9).Font.Bold = True
Cells(x, 10).Value = mydate2 - datetoday2
End If
Next
Set myApp = Nothing
Set mymail = Nothing
End Sub
下面的代码行处理代表邮件正文的 HTML 文档:
.HTMLBody = .HTMLBody & strbody
如果您想向现有邮件正文添加内容,则需要在开始 <body>
和结束 </body>
标记之间插入您的字符串。
此外,对于 HTML 标记,以下字符串的格式不正确:
strbody = "Good Morning" & vbCrLf & _
"Please be aware that this file is due for review in the next 30 days" & vbCrLf & _
"Please click <a href=" & Cells,(x, 13) & " >here</ a>" & vbCrLF & _
"Regards" & vbCrLf & _
"Master Control Document"
我在这个特定查询上花了 3 天时间,所以非常感谢任何帮助。 我创建了一个宏,可以在电子表格中的某个日期前 30 天向特定的人发送电子邮件,将文档的标题放在主题中,并标记我的电子表格以标记为电子邮件已发送。工作完美!但是,我想将 link 从电子表格中的 link 放入电子邮件中,这样电子邮件收件人只需单击 link 即可直接转到文档进行审阅。我正在尝试使用 href 和 HTMLBody 函数,但没有成功,只是费了很大力气。如果有人能给我任何帮助,那就太好了。我附上了我正在使用的代码。正如我所说,在我使用 strbody 之前效果很好。我只是无法弄清楚我在这里做错了什么。非常感谢
Sub datesexcelvba()
Dim myApp As Outlook.Application, mymail As Outlook.MailItem
Dim mydate1 As Date
Dim mydate2 As Long
Dim datetoday1 As Date
Dim datetoday2 As Long
Dim strbody As String
Dim x As Long
lastrow = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
For x = 2 To lastrow
mydate1 = Cells(x, 8).Value
mydate2 = mydate1
Cells(x, 11).Value = mydate2
datetoday1 = Date
datetoday2 = datetoday1
Cells(x, 12).Value = datetoday2
If mydate2 - datetoday2 = 30 Then
Set myApp = New Outlook.Application
Set mymail = myApp.CreateItem(olmailitem)
strbody = "Good Morning" & vbCrLf & _
"Please be aware that this file is due for review in the next 30 days" & vbCrLf & _
"Please click "<a href= Cells,(x, 13)" >here</ a>" &vbCrLF & _
"Regards" & vbCrLf & _
"Master Control Document"
mymail.To = Cells(x, 7).Value
With mymail
.Subject = "Review Reminder " & Cells(x, 4).Value
.HTMLBody = .HTMLBody & strbody
.Display
'.send
End With
Set myApp = Nothing
Set mymail = Nothing
Cells(x, 9) = "Yes"
Cells(x, 9).Font.ColorIndex = 3
Cells(x, 9).Font.Bold = True
Cells(x, 10).Value = mydate2 - datetoday2
End If
Next
Set myApp = Nothing
Set mymail = Nothing
End Sub
下面的代码行处理代表邮件正文的 HTML 文档:
.HTMLBody = .HTMLBody & strbody
如果您想向现有邮件正文添加内容,则需要在开始 <body>
和结束 </body>
标记之间插入您的字符串。
此外,对于 HTML 标记,以下字符串的格式不正确:
strbody = "Good Morning" & vbCrLf & _
"Please be aware that this file is due for review in the next 30 days" & vbCrLf & _
"Please click <a href=" & Cells,(x, 13) & " >here</ a>" & vbCrLF & _
"Regards" & vbCrLf & _
"Master Control Document"