Outlook VBA code for HTML newsletter

This is an MS Outlook 2013 code to insert an HTML file into an email. File can have CSS definitions and external links to photos.

1- Go to file -> Options.2- In the “Customize Ribbon” section, check “Developer” in the Customize the Ribbon / Main Tabs panel (right) – Click OK
3- Open the Developer tab in the Ribbon. Click on Macros, Macros, Type “InsertHTML” and Create
4- In the Microsoft VBA window, click on tools -> References and make sure Microsoft Office 15.0 Object Library and Microsoft Word 15.0 Object Library are both checked- press OK
5- Copy and paste the following code:

Sub InsertHTML()
Dim insp As Inspector
Set insp = ActiveInspector
If insp.IsWordMail Then
Dim wordDoc As Word.Document
Set wordDoc = insp.WordEditor
Dim FileToOpen As String
FileToOpen = InputBox(“filename?”)
wordDoc.Application.Selection.InsertFile FileToOpen, , False, False, False
End If
End Sub

6- Save and close the Microsoft VBA editor
7- In the main menu, click on the developer tab in the ribbon, click on “Macro Security”. Select “Notification for all Macros”.8- Open a new message. Place the cursor in the empty message, go to Developer, click on Macro and select the Project1.InsertHTML macro.
9- When prompted enter the HTML file to import location.

 

Facebooktwitter Facebooktwitter

9 Replies to “Outlook VBA code for HTML newsletter”

    1. Hello Blake,

      Sorry for the late reply. Can you please check my previous comments related to references and typing the full path for the file?

  1. Hi there. I tried this today & got this error “Object variable or With block variable not set”. Where did I go wrong?

    1. Hello Sri,

      Sorry for the late reply. Can you check my previous comments on the subject. Does this help?
      Can you send me more information? (screenshot / other)

    1. Hello Suave,

      Is this happening after you run the macro? Are you typing the full path to the file? e.g. c:\user\me\documents\my.html?

      Please check my reply to Daniel as this may be due to unchecked references.

  2. Dear Nasri,

    I’m sure I used to use this macro to send out HTML via Outlook 2013. However, with a new install, I can’t get it to work, either with the old OTM from my last install, or by creating everything from scratch. In the editor the line FileToOpen = InputBox(“filename?”) is red, and I get the error message “Compile error: syntax error”. Is there any chance you could give any suggestions?

    1. Hello Daniel,

      Open the macro in the VBA editor. Select menu option “Tools > References” and make sure the following references are checked:
      – Visual Basic for Applications
      – Microsoft Outlook 15.0 Object Library
      – OLE Automation
      – Microsoft Office 15.0 Object Library
      – Microsoft Word 15.0 Object Library

Leave a Reply

Your email address will not be published. Required fields are marked *