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

我在Excel中创build了一个macros,每次更新特定文件时都会向各种用户发送电子邮件。

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文件是一个可点击的链接到网站。

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

  1. Body属性返回或设置表示Outlook项目的明文正文的string。
  2. MailItem类的HTMLBody属性返回或设置表示指定项目的HTML正文的string。 设置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

  1. Word对象模型可以用于处理消息体。 有关更多信息,请参见第17章:使用项目组 。

请注意, MailItem.BodyFormat属性允许您以编程方式更改用于项目主体的编辑器。

最后两个支持在消息体中创build一个超链接。 这取决于你select哪种方式。

如果你想这样做,你将不得不写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>