Home » excel » Excel VBA URL hyperlink in RichText email body

Excel VBA URL hyperlink in RichText email body

Posted by: admin April 23, 2020 Leave a comment

Questions:

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 = "[email protected]"
    .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

How to&Answers:

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.

Answer:

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