简体   繁体   中英

Excel VBA URL hyperlink in RichText email body

Does anyone know how to create a RichText email body, that has a URL bookmark in it?

For example, here is my code:

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 = "someone@someone.com"
    .Subject = "Email Subject"

    Set olInsp = .GetInspector
    Set wdDoc = olInsp.WordEditor

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

Many thanks for your help :) Dave

After Dmitry pointing me in the right direction, I tried the following code:

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

and it has solved my problem!

I wasn't considering the word.document object when creating the email body, when I should have been. Hope this helps someone else.

There's small change in your code.

You need to add BODYFORMAT as

.BodyFormat = olFormatHTML

After setting BODYFORMAT , set the HTMLBODY of the Email.

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

Check this link for complete example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM