2015-01-14 88 views
2

有谁知道如何创建一个RichText电子邮件正文,该正文中包含URL书签?RichText电子邮件正文中的Excel VBA URL超链接

例如,这里是我的代码:

Sub SendEmailUsingWord() 

Dim olApp As Outlook.Application 
Dim olEmail As Outlook.MailItem 
Dim olInsp As Outlook.Inspector 
Dim wdDoc As Word.Document 
Dim strBody As String 

' The body of the message, want to create a hyperlink to (e.g.) http://www.google.com but 
' still display HERE. 
' In HTML it would be <a href="http://www.google.com">HERE</a> 
' But I want to achieve this in a Rich Text email 

strBody = "Please click HERE to be fantastic!" & vbNewLine 

Set olApp = New Outlook.Application 
Set olEmail = olApp.CreateItem(olMailItem) 

With olEmail 
    .BodyFormat = olFormatRichText 
    .Display 

    .To = "[email protected]" 
    .Subject = "Email Subject" 

    Set olInsp = .GetInspector 
    Set wdDoc = olInsp.WordEditor 

    wdDoc.Range.InsertBefore strBody 
    '.send 
End With 
End Sub 

非常感谢您的帮助:) 戴夫

+0

您是否特别想要RTF,或者只要您获得链接,它就可以是HTML? –

+0

嗨德米特里。我想使用RTF,但如果它太复杂,那么我将不得不将代码转换为使用HTMLbody。 – Dave

+0

您仍然可以使用RTF,但您需要使用RtfBody属性或使​​用Word文档对象。上面的代码出了什么问题? –

回答

0

有没有在你的代码小的变化。

您需要添加BODYFORMAT作为

.BodyFormat = olFormatHTML 

设置BODYFORMAT后,设置电子邮件的HTMLBODY

.HTMLBody = "<HTML><H2>The body of this message will appear in HTML.</H2><BODY>Type the message text here. </BODY></HTML>" 

检查此链接为完整的例子:

http://msdn.microsoft.com/en-us/library/office/ff869979(v=office.15).aspx

+0

谢谢帕雷什。不幸的是,我正在寻找仅使用RTF格式的邮件正文,因为我将从电子表格的其他部分粘贴一些单元格(已格式化)。否则,我会在HTMLbody中完成所有工作,但需要知道是否可以在RTF主体中执行所尝试的操作。 – Dave

1

梅德指着我在正确的方向后,我尝试下面的代码:

wdDoc.Hyperlinks.Add wdDoc.Range, "http://www.google.com", , , "HERE"

,它有解决了我的问题!

我在创建电子邮件正文时并没有考虑word.document对象,因为我应该这样做。希望这可以帮助别人。

+0

伟大的工作!自己解决。 +1。 –

相关问题