VBA Outlook 如何在电子邮件正文中添加超链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23268642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:52:31  来源:igfitidea点击:

VBA Outlook How to add hyperlink into email body

vbawinapioutlook-vba

提问by Qbik

This macro adds hyperlink to email :

此宏向电子邮件添加超链接:

  Sub test_add_hyperlink()
     Dim NewMail As Outlook.MailItem
     Set NewMail = Application.ActiveInspector.CurrentItem
     NewMail.HTMLBody = "<HTML><BODY><A href=http://www.someaddress.com>URL_TEXT</A></BODY></HTML>" & NewMail.HTMLBody  End Sub

but how to add hyperlink in place where active cursor is ? I ask beacause I would like to add hyperlink not at the front of message, but where my currently writing message.

但是如何在活动光标所在的位置添加超链接?我问是因为我不想在消息的前面添加超链接,而是在我当前编写消息的位置。

The hyperlink I would like to add is the hyperlink to file which is currently copied to Windows' clipboard, this part I've written, but I can't figure out how to place it not at the front of email, but in place where active cursor is. I think that macro based emulation of Windows' keypressing is one of the directions to follow.

我想添加的超链接是当前复制到 Windows 剪贴板的文件的超链接,这部分是我写的,但我不知道如何将它放在电子邮件的前面,而是放在活动光标是。我认为基于宏的 Windows 按键模拟是要遵循的方向之一。

回答by niton

This describes how to paste at the selection.

这描述了如何在选择处粘贴。

http://www.slipstick.com/developer/code-samples/paste-formatted-text-vba/

http://www.slipstick.com/developer/code-samples/paste-formatted-text-vba/

Sub PasteFormattedClipboard()

    Dim objItem As Object
    Dim objInsp As Outlook.Inspector
    Dim objWord As Word.Application
    Dim objDoc As Word.Document
    Dim objSel As Word.Selection

    Set objItem = Application.ActiveInspector.CurrentItem
    Set objInsp = objItem.GetInspector
    Set objDoc = objInsp.WordEditor
    Set objWord = objDoc.Application
    Set objSel = objWord.Selection

    objSel.PasteAndFormat (wdFormatOriginalFormatting)

    Set objItem = Nothing
    Set objInsp = Nothing
    Set objDoc = Nothing
    Set objWord = Nothing
    Set objSel = Nothing

 End Sub

回答by Heinz123

Sub InsertHyperlinkAtCursorPositon()
On Error GoTo finish
strLink = "http://www.outlookcode.com"
strLinkText = "Get Outlook code samples here"
Set objInsp = Application.ActiveInspector
Set objMsg = objInsp.CurrentItem
Set objDoc = objInsp.WordEditor
Set objSel = objDoc.Windows(1).Selection
If objMsg.BodyFormat <> olFormatPlain Then
    objDoc.Hyperlinks.Add objSel.Range, strLink, _
                          "", "", strLinkText, ""
Else
    objSel.InsertAfter strLink
End If
finish:
End Sub