Vba code to bold text in email body
Here is an example of VBA code that can be used to bold text in the body of an email:
Sub BoldTextInEmailBody()
Dim olApp As Object
Dim olMail As Object
Dim body As String
Dim boldText As String
' Set up the Outlook application and email object
Set olApp = CreateObject("Outlook.Application")
Set olMail = olApp.CreateItem(0) ' 0 = email
' Set the email body
body = "This is the email body text."
' Set the bold text
boldText = "This text will be bold."
' Use the Range object to select the text and apply bold formatting
With olMail.Body
.Range.Find(what:=boldText, MatchCase:=True).Select
Selection.Font.Bold = True
End With
' Send the email
olMail.Send
' Clean up
Set olMail = Nothing
Set olApp = Nothing
End Sub
This code uses the Outlook.Application
and Outlook.MailItem
objects to create a new email and set its body text. It then uses the Range
object to select the text that should be bolded, and applies bold formatting using the Font.Bold
property.
You can modify the code to suit your specific needs, such as changing the text that should be bolded or applying different formatting options.
To use this code, follow these steps:
- Open the Visual Basic Editor in Outlook by pressing
Alt + F11
or by navigating toDeveloper
>Visual Basic
in the ribbon. - In the Visual Basic Editor, click
Insert
>Module
to insert a new module. - Paste the code into the module.
- Click
Run
>Run Sub/User Form
to run the code. - The code will create a new email with the specified body text and bold the specified text.
Note: This code uses late binding, which means that it does not require a reference to the Outlook object library. However, if you prefer to use early binding, you can modify the code to use the Outlook.Application
and Outlook.MailItem
objects explicitly, like this:
Dim olApp As Outlook.Application
Dim olMail As Outlook.MailItem
You will also need to set a reference to the Outlook object library in the Visual Basic Editor by navigating to Tools
> References
and checking the box next to Microsoft Outlook XX.X Object Library
, where XX.X
is the version of Outlook you are using.