2017-10-13 50 views
2

我在Excel中創建了一個宏,每次更新特定文件時都會向各種用戶發送電子郵件。如何將超鏈接插入電子郵件正文

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) 

Dim answer As String 

answer = MsgBox("Would you like to save the changes?", vbYesNo, "Save Document") 

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 ("Name1") 
    newmsg.Recipients.Add ("[email protected]") 
    'newmsg.Recipients.Add ("Name2") 
    newmsg.Recipients.Add ("[email protected]") 
    'add subject 
    newmsg.Subject = "Notification - Update file" 
    'add body 
    newmsg.Body = "This is an automated notification." & vbNewLine & vbNewLine & _ 
     "The XXX file has been recently updated" & vbNewLine & vbNewLine & _ 
     "Please do not reply to this email." 
    newmsg.Display 'display 
    newmsg.Send 'send message 
    'give conformation of sent message 
    MsgBox "Your document has successfully been saved", , "Confirmation" 
End If 

'save the document 
'Me.Worksheets.Save 

End Sub 

我想超鏈接添加到正文它說:「該XXX文件最近已經更新」,使XXX文件是一個可點擊的鏈接到一個網站。

回答

1

Outlook對象模型支持用於定製消息體三種主要方式:

  1. Body屬性返回或設置表示Outlook項目的明文體的字符串。
  2. 所述的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 = "Enter the message text here. " .Display End With End Sub

  • Word對象模型可被用於處理郵件的正文。有關更多信息,請參閱Chapter 17: Working with Item Bodies
  • 注意,MailItem.BodyFormat屬性允許您以編程方式更改用於項目主體的編輯器。

    最後兩個支持創建在郵件正文中的超鏈接。這取決於你選擇哪種方式。

    +0

    感謝您的詳細解釋,尤金。我只是開始使用vba代碼,所以總是很好的理解這些邏輯。 – IdCB

    1

    如果你想做到這一點,你必須編寫HTML而不是純文本。 這條線:

    newmsg.Body = "The XXX file has been recently updated" 
    

    ...會變成這樣的:

    newMsg.HTMLBody = "The <a href=" & """" & "http://www.yourlink.com" & """" & ">XXX file</a> has been recently updated". 
    

    這是因爲在Outlook電子郵件與格式化你寫HTML文本,並在HTML鏈接表示如下:

    <a href="yourlink">your Hyper-text</a> 
    
    +0

    感謝您的回答。我遵循你的指示,並得到了我想要的。 – IdCB

    +0

    @IdCB在這種情況下,請接受答案,以便線程關閉 –

    相關問題