簡體   English   中英

Excel vba,使包含超鏈接的單元格出現在電子郵件正文中

[英]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 newmsgHTMLbody屬性,並將您的信息解析為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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM