How To Programmatically Add a Hyperlink to an HTML Message (230915)



The information in this article applies to:

  • Microsoft Outlook 2000
  • Microsoft Outlook 98

This article was previously published under Q230915

SUMMARY

This article explains how to programmatically add an embedded hyperlink to a new message without clicking the body of the message to format the link as a valid hyperlink. To perform this, you must provide the HTML format codes in the string you write to the HTMLBody property of the message.

MORE INFORMATION

The HTMLBody property is available from the MailItem object in the following Microsoft Outlook object models:
  • Microsoft Outlook 98 (Msoutl85.olb)
  • Microsoft Outlook 2000 (Msoutl9.olb)
If the default EditorType property of the mail client is not in HTML format, then the HTMLBody property does not preserve the HTML formatting. The data is converted to either Rich Text Format (RTF) or Text, depending on the EditorType property, and then saved in the Body property.

The following Visual Basic sample demonstrates writing HTML formatted text to a new message using the HTMLBody property:

'Requires a reference to the Outlook Object Library
Dim spApplication as Outlook.Application
Dim spMailItem as Outlook.MailItem

Set spApplication = New Outlook.Application
Set spMailItem = spApplication.CreateItem(olMailItem)
If Not spMailItem Is Nothing Then
 If spMailItem.GetInspector.EditorType = olEditorHTML Then
  spMailItem.HTMLBody = spMailItem.HTMLBody & _
  "<br><a href=""http://www.microsoft.com""><small>Inserted Hyperlink</small></a><br>"
  'HTML formatted text
 Else
  spMailItem.Body = spMailItem.Body & "Inserted Text"
 End If
 spMailItem.Save
 spMailItem.Close(olSave)
 Set spMailItem = Nothing
End If
Set spApplication = Nothing
				


Modification Type:MinorLast Reviewed:7/1/2004
Keywords:kbhowto kbMsg kbOutlookObj KB230915