excel - 如何在电子邮件正文中插入超链接

标签 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 文件成为指向网站。

最佳答案

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

  1. Body属性返回或设置表示 Outlook 项目的明文正文的字符串。
  2. HTMLBody MailItem 类的属性返回或设置表示指定项目的 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 = "<HTML><BODY>Enter the message <a href="http://google.com">text</a> here. </BODY></HTML>" 
        .Display 
       End With 
     End Sub
  • Word 对象模型可用于处理消息正文。请参阅Chapter 17: Working with Item Bodies了解更多信息。
  • 注意,MailItem.BodyFormat属性允许您以编程方式更改用于项目正文的编辑器。

    最后两个支持在消息正文中创建超链接。选择哪种方式取决于您。

    关于excel - 如何在电子邮件正文中插入超链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46736021/

    相关文章:

    javascript - <a> 和 <button> 用于创建链接时有性能差异吗?

    python - 如何从 Excel 中访问值,其中第一列是标题

    excel - 如何在 maatwebsite 3.1 的多个表单中使用 withEvents 应用样式?

    VBA 如何使用字符串变量循环 instr 函数?

    移动图像后,HTML + CSS 图像链接将不起作用

    c# - 在 C#/ASP.NET 中有条件地显示图标

    c# - 包含来自 SSRS 报告的多个工作表的 Excel 文件

    excel - 获取文件路径(以文件夹结尾)

    excel - 自定义函数数据类型错误 - 为什么?还有如何调试?

    vba - 清除未 protected 单元格的按钮