A user wanted to reply to senders by inserting a Word document into the response as the message text. While you can do this using copy and paste, or using Insert > Insert As Text, you can automate it using VBA.
To do this, you use the Word Object Model to open and copy the document then use the WordEditor object in Outlook to paste the copied content.
You need to set a reference to the Word Object Library to use this macro. Go to Tools, References menu. Locate the Word object library in the list and add a check mark to it.
As written, the reply macros work on the message selected in the message list. Either can be tweaked to work on multiple selected messages. At the end is a macro to send a new message.
Include Original Message
This macro creates the reply and inserts the document contents at the top of the reply before the Outlook signature.
Sub EmailFromDoc() Dim wordApp As Object, editor As Object Dim wordDoc As Object Dim m As MailItem ' Object to represent the selected email Dim reply As MailItem ' Object for the reply email Dim olEditor Set wordApp = CreateObject("Word.Application") Set wordDoc = wordApp.Documents.Open("D:\Documents\Test.docx") wordDoc.Content.Copy wordDoc.Close Set m = Application.ActiveExplorer.Selection(1) Set reply = m.reply With reply .BodyFormat = olFormatHTML .Display Set olEditor = .GetInspector.WordEditor Set olSelect = olEditor.Application.Selection 'olEditor.Range(0, 0).InsertParagraphBefore olSelect.PasteAndFormat (wdFormatOriginalFormatting) ' .Send End With Set wordApp = Nothing End Sub
If you receive a "User-defined type not defined" error, you did not set the reference to the Word object library.
Don't Include Original Message
This version of the macro creates the message without the original message included. The signature is also removed.
Sub EmailFromDocNoOriginal() Dim wordApp As Object, editor As Object Dim wordDoc As Object Dim m As MailItem ' Object to represent the selected email Dim reply As MailItem ' Object for the reply email Dim olEditor Set wordApp = CreateObject("Word.Application") Set wordDoc = wordApp.Documents.Open("D:\Documents\Test.docx") wordDoc.Content.Copy wordDoc.Close Set m = Application.ActiveExplorer.Selection(1) Set reply = m.reply With reply .BodyFormat = olFormatHTML Set olEditor = .GetInspector.WordEditor olEditor.Content.Paste .Display '.Send End With Set wordApp = Nothing End Sub
Send a New Message
This version of the macro create a new message with the content of the document inserted.
Sub NewEmailFromDoc() Dim wordApp As Object, editor As Object Dim wordDoc As Object Dim olMail As MailItem ' Object to represent the selected email Dim olEditor Set wordApp = CreateObject("Word.Application") Set wordDoc = wordApp.Documents.Open("F:\Documents\Test.docx") wordDoc.Content.Copy wordDoc.Close Set olMail = Application.CreateItem(olMailItem) With olMail .BodyFormat = olFormatHTML .Display .To = "test@domain.com" .Subject = "Cute puppies" Set olEditor = .GetInspector.WordEditor Set olSelect = olEditor.Application.Selection With olSelect ' to delete the signature '.WholeStory '.Expand Unit:=wdStory '.Delete .PasteAndFormat (wdFormatOriginalFormatting) End With ' .Send End With Set wordApp = Nothing End Sub
How to use the macros on this page
First: You need to have macro security set to the lowest setting, Enable all macros during testing. The macros will not work with the top two options that disable all macros or unsigned macros. You could choose the option Notification for all macros, then accept it each time you restart Outlook, however, because it's somewhat hard to sneak macros into Outlook (unlike in Word and Excel), allowing all macros is safe, especially during the testing phase. You can sign the macro when it is finished and change the macro security to notify.
To check your macro security in Outlook 2010 and newer, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, look at Tools, Macro Security.
After you test the macro and see that it works, you can either leave macro security set to low or sign the macro.
The macros on this page should be placed in a module.
Open the VBA Editor by pressing Alt+F11 on your keyboard.
To put the code in a module:
- Right click on Project1 and choose Insert > Module
- Copy and paste the macro into the new module.
To put the macro code in ThisOutlookSession:
- Expand Project1 and double click on ThisOutlookSession.
- Copy then paste the macro into ThisOutlookSession. (Click within the code, Select All using Ctrl+A, Ctrl+C to copy, Ctrl+V to paste.)
Set a reference to other Object Libraries
If you receive a "User-defined type not defined" error, you need to set a reference to another object library.
- Go to Tools, References menu.
- Locate the object library in the list and add a check mark to it. (Word and Excel object libraries version numbers will match Outlook's version number.)
Commonly used libraries are Microsoft Word, Microsoft Excel, Microsoft Forms, and VBScript Regular Expression.
More information as well as screenshots are at How to use the VBA Editor