如何将超链接插入电子邮件正文

时间:2017-10-13 18:29:48

标签: excel vba hyperlink outlook

我在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@xxx.com")
    'newmsg.Recipients.Add ("Name2")
    newmsg.Recipients.Add ("email@xxx.com")
    '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文件是指向a的可点击链接网站。

2 个答案:

答案 0 :(得分:1)

如果你想这样做,你必须写HTML而不是纯文本。 这一行:

Context

......会变得像:

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

这是因为在带有格式的Outlook电子邮件中,您可以编写HTML文本,HTML中的链接表示如下:

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

答案 1 :(得分:1)

Outlook对象模型支持三种自定义邮件正文的主要方法:

  1. Body属性返回或设置表示Outlook项目的明文正文的字符串。
  2. MailItem类的HTMLBody属性返回或设置表示指定项的HTML正文的字符串。设置HTMLBody属性将始终立即更新Body属性。例如:
  3. 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

    1. Word对象模型可用于处理邮件正文。有关详细信息,请参阅Chapter 17: Working with Item Bodies
    2. 注意,MailItem.BodyFormat属性允许您以编程方式更改用于项目正文的编辑器。

      最后两个支持在邮件正文中创建超链接。这取决于你选择哪种方式。