简体   繁体   中英

Excel vba, Make cell containing hyperlink appear in email body

I have a code that is sending an email to users when a submittal date is added. In the body of this email I am referring to a cell that contains a hyperlink to a folder. the destination of this hyperlink is appearing but it is not active, meaning it appears only as text. Here is my code, and the hyperlink cell is referred to in the 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

Explanatory Image

At the moment the e-mail you're creating is Plain Text, and because of this it's essentially down to the client receiving the e-mail as to whether or not it will automatically attempt to turn things which look like they should be hyperlinks into actual hyperlinks. A file path in the format C:\\Path\\to\\file.xls is not something this would ordinarily apply to.

The proper way to do this would be to use the HTMLbody property of your MailItem newmsg , and to parse your information into HTML specifying what exactly should be a hyperlink.

A slightly 'cheaty' but quite a simple way of doing this using only Plain Text would be to amend your path slightly. If you append file:// to your path some e-mail clients including Outlook will automatically create a hyperlink for you. So, for example, you would change your newmsg.Body line to read:

newmsg.Body = "Dear User, New Submittal ( file://" & SubmitLink & " ) has been Added in submittal Log. Please Investigate the Change" & vbLf & vbLf & vbLf & "Sincerely,"

Which would produce a hyperlink like file://C:\\Path\\to\\file.xls . Obviously if the e-mail client your recipients are using does not create automatic hyperlinks as I've described here, then you will need to build a HTMLBody to get hyperlinks working correctly.

Edit in response to comment , to give a brief explanation of how you would use HTMLBody . Instead of setting newmsg.Body you instead set newmsg.HTMLBody to your HTML formatted message. In the very simplest of terms you could build your existing message as follows.

    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

I would suggest taking at a look W3Schools if you want to learn further about building HTML.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM