2017-10-13 51 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在这种情况下,请接受答案,以便线程关闭 –

    相关问题