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

时间:2014-04-24 12:17:02

标签: vba winapi outlook-vba

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

  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

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

我要添加的超链接是当前复制到Windows剪贴板的文件超链接,这部分我已经写过了,但我无法弄清楚如何将它放在电子邮件的前面,但是在活动光标所在的位置。我认为基于宏的Windows模拟按键仿真是要遵循的方向之一。

2 个答案:

答案 0 :(得分:2)

这描述了如何粘贴选择。

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

答案 1 :(得分:2)

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