如何添加不同的超链接到发送给不同人的 Excel 电子邮件正文?每封电子邮件都有不同的超链接。这是我到目前为止的代码:
Sub Button1_Click()
Dim rngCell As Range
Dim Rng As Range
Dim OutApp As Object
Dim OutMail As Object
Dim strBody As String
Dim EmailSubject As String
Dim SendToMail As String
Dim r As Long
Application.ScreenUpdating = False
With ActiveSheet
If .FilterMode Then .ShowAllData
End With
Set OutApp = CreateObject("Outlook.Application")
Set Rng = Range("T5", Cells(Rows.Count, "T").End(xlUp))
For Each rngCell In Rng
r = rngCell.Row
If Range("J" & r).Value = "" And Range("K" & r).Value <> "" And Range("I" & r).Value <= Date Then
Range("J" & r).Value = Date
Set OutMail = OutApp.CreateItem(0)
strBody = "According to my records, your " & Range("A" & r) & Range("S" & r).Value & _
" contract is due for review. This contract expires " & Range("K" & r).Value & _
". It is important you review this contract ASAP and email me " & _
"with any changes that are made. If it is renewed or rolled over, please fill out the " & _
"Contract Cover Sheet which can be found in the Everyone folder " & _
"and send me the Contract Cover Sheet along with the new original contract."
SendToMail = Range("T" & r).Value
EmailSubject = Range("A" & r).Value
On Error Resume Next
With OutMail
.To = SendToMail
.CC = "email address removed for privacy reasons"
.BCC = ""
.Subject = EmailSubject
.Body = strBody
.Display ' You can use .Send
End With
End If
Next rngCell
Application.ScreenUpdating = True
End Sub
在代码中,您正在处理纯文本消息正文:
.Body = strBody
要设置超链接,您需要为消息正文准备 HTML 标记,然后设置 HTMLBody 属性,该属性返回表示指定项目的 HTML 正文的字符串。
Outlook 对象模型支持三种主要的自定义邮件正文的方式:
MailItem
类的HTMLBody属性返回或设置表示指定项目的HTML正文的字符串。设置
HTMLBody
属性将始终立即更新 Body 属性。例如: Sub CreateHTMLMail()
'Creates a new e-mail item and modifies its properties.
Dim objMail As Outlook.MailItem
'Create e-mail item
Set objMail = Application.CreateItem(olMailItem)
With objMail
'Set body format to HTML
.BodyFormat = olFormatHTML
.HTMLBody = "<HTML><BODY>Enter the message <a href="http://google.com">text</a> here. </BODY></HTML>"
.Display
End With
End Sub
您应该能够添加我猜测 Excel 工作表单元格中已经存在的超链接。如果链接的排列方式使其位于名称行旁边的单元格中,则您可以仅使用当前的 for 循环。只需添加 & Range("在此处输入带有超链接的单元格").value。对于大部分来说应该不是问题。
如果超链接只有几个,并且需要检查发送给谁,则可以在 for 循环中使用 IF 条件。