[英]Excel vba, Make cell containing hyperlink appear in email body
我有一个添加提交日期后向用户发送电子邮件的代码。 在此电子邮件的正文中,我指的是一个包含指向文件夹的超链接的单元格。 该超链接的目的地出现了,但是它没有激活,这意味着它仅以文本形式出现。 这是我的代码,在电子邮件正文中引用了超链接单元格
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
' The variable KeyCells contains the cells that will
' cause an alert when they are changed.
Set KeyCells = Range("J3:J1000")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
' Display a message when one of the designated cells has been
' changed.
' Place your code here.
Dim answer As String
Dim SubmitLink As String
SubmitLink = Target.Offset(, -8).Value
answer = MsgBox("Do you wish to save this change. An Email will be sent to the User", vbYesNo, "Save the change")
If answer = vbNo Then Cancel = True
If answer = vbYes Then
'open outlook type stuff
Set OutlookApp = CreateObject("Outlook.Application")
Set OlObjects = OutlookApp.GetNamespace("MAPI")
Set newmsg = OutlookApp.CreateItem(olMailItem)
'add recipients
'newmsg.Recipients.Add ("Name Here")
newmsg.Recipients.Add Worksheets("Coordinator").Range("Q4").Value
'add subject
newmsg.Subject = Worksheets("Coordinator").Range("O3").Value
'add body
newmsg.Body = "Dear User, New Submittal ( " & SubmitLink & " ) has been Added in submittal Log. Please Investigate the Change" & vbLf & vbLf & vbLf & "Sincerely,"
newmsg.Display 'display
newmsg.Send 'send message
'give conformation of sent message
MsgBox "Modification confirmed", , "Confirmation"
End If
' MsgBox "Cell " & Target.Address & " has changed."
End If
End Sub
目前,您正在创建的电子邮件是纯文本,因此,这基本上取决于客户端是否会自动尝试将看起来像是超链接的内容转换为电子邮件的客户端。实际的超链接。 格式为C:\\Path\\to\\file.xls
的文件路径通常不适用。
正确的方法是使用MailItem
newmsg
的HTMLbody
属性,并将您的信息解析为HTML,从而指定确切的超链接。
仅使用纯文本的一种“小技巧”但相当简单的方法是稍微修改路径。 如果将file://
附加到路径,则某些电子邮件客户端(包括Outlook)将自动为您创建超链接。 因此,例如,您将newmsg.Body
行更改为:
newmsg.Body = "Dear User, New Submittal ( file://" & SubmitLink & " ) has been Added in submittal Log. Please Investigate the Change" & vbLf & vbLf & vbLf & "Sincerely,"
这将产生像file://C:\\Path\\to\\file.xls
这样的超链接。 显然,如果收件人使用的电子邮件客户端没有创建自动超链接,如我在此所述,那么您将需要构建HTMLBody
才能使超链接正常工作。
根据评论进行 编辑 ,以简要说明如何使用HTMLBody
。 无需设置newmsg.Body
,而是将newmsg.HTMLBody
设置为HTML格式的消息。 用最简单的术语,您可以按如下方式构建现有消息。
Dim emailBody As String
' At the very minimum your basic HTML page is made up of:
' <html>
' <body>
' Your message, including any formatting or hyperlniks.
' </body>
' </html>
'
emailBody = "<html><body><font face=""Arial, sans-serif"">"
emailBody = emailBody & "Dear User, New Submittal "
emailBody = emailBody & "<a href=""" & SubmitLink & """>" & SubmitLink & "</a>"
emailBody = emailBody & " has been Added in submittal Log. Please Investigate the Change<br><br><br>Sincerely,"
emailBody = emailBody & "</font></body></html>"
newmsg.HTMLBody = emailBody
如果您想进一步学习构建HTML,我建议您看看W3Schools 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.