Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert text into Outlook email editor using VBA

I wish to create a macro that inserts the date into an email body which is currently open for editing, prior to sending.

I am using Outlook 2013 on a Windows 7 machine.

Desired workflow:

  • Click reply to an email
  • Within the email editor, place the text cursor within email body for desired position to insert text
  • Execute the macro (by clicking on an icon in the email editor window's quick access toolbar). Macro will insert the date.

(Assume the date has been stored as a string variable, i.e. the macro simply inserts a variable into the email body at a desired position).

All Outlook VBA examples I have found for inserting text into an email body involve storing the active email's HTMLBody as a string, appending the desired text to that string, then creating a brand new email, and re-populating the to, cc, bcc, subject and htmlbody. I wish to avoid this, as it seems very clunky.

Thank you in advance for your help.

like image 887
Marty131 Avatar asked Oct 30 '25 01:10

Marty131


1 Answers

InsertBefore Method or InsertAfter Method

Inspector.WordEditor Property (Outlook)

Application.ActiveInspector Method (Outlook)

Example

Option Explicit
Public Sub Example()
    Dim Inspector As Outlook.Inspector
    Dim wdDoc As Word.Document
    Dim Selection As Word.Selection
        

    Set Inspector = Application.ActiveInspector()
    Set wdDoc = Inspector.WordEditor
    Set Selection = wdDoc.Application.Selection
        Selection.InsertBefore Format(Now, "DD/MM/YYYY")
    
    
    Set Inspector = Nothing
    Set wdDoc = Nothing
    Set Selection = Nothing
End Sub

enter image description here


Reference to Microsoft Word xx.x Object Library


Go to Outlook VBA editor either by pressing "Alt + F11" keys or clicking on the "Visual Basic" button in the “Developer” ribbon.

  • 1. In VBA editor window, click the "Tools" button in the menu bar.

  • 2. Then, from the drop down list, select the "References" option.

enter image description here

  • 3. In the dialog box, you can pull the scrolling bar down until you locate what you want, such as "Microsoft Word XX.X Object Library".

enter image description here

like image 95
0m3r Avatar answered Oct 31 '25 19:10

0m3r



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!