Vba code to add hyperlink in email body
Here is an example of VBA code that adds a hyperlink to the body of an email:
Sub AddHyperlinkToEmailBody()
Dim olApp As Object
Dim olMail As Object
Dim hyperlink As String
' Set the email address and subject
hyperlink = "https://www.example.com"
emailSubject = "Test Email with Hyperlink"
emailBody = "This is a test email with a hyperlink: " & hyperlink
' Create a new email object
Set olApp = CreateObject("Outlook.Application")
Set olMail = olApp.CreateItem(0)
' Set the email properties
With olMail
.Subject = emailSubject
.Body = emailBody
.To = "[email protected]"
End With
' Add the hyperlink to the email body
olMail.HTMLBody = olMail.HTMLBody & "<a href='" & hyperlink & "'>Click here</a>"
' Send the email
olMail.Send
' Clean up
Set olMail = Nothing
Set olApp = Nothing
End Sub
This code uses the Outlook Object Library to create a new email object and set its properties. It then adds the hyperlink to the email body using HTML code. Finally, it sends the email using the Send
method.
You can modify the code to suit your needs, such as changing the email address, subject, and body, or adding additional hyperlinks.
Note: This code assumes that you have the Outlook Object Library referenced in your VBA project. If you don't have it referenced, you can add it by going to Tools > References > Microsoft Outlook Object Library.
Also, this code is written in VBA and is intended to be used in an Excel worksheet or a Word document. If you want to use this code in a different context, you may need to modify it accordingly.