Send Word document as email body

I already explained how to send emails automatically, but didn’t really focus on the content of the email. That is probably biggest problem of this method followers. In this article I’m going to show You how to cope with such issue and send Word document as email body.

The easiest way to have the content formatted properly is to copy text from Word and paste that to the created email. You cannot pass the content as string variable, because You will loose all the formatting.

Also there are other possibilities like adding HTML tags into the content, but this solution is good, when You have only 1 text. Not changeable, static, You know. And if it’s really long it would take a lot of time for tagging…

Anyway, back to the main topic, Word content to email body!

First of all, You need to copy the Word file content into the clipboard. To do that You need to create Word object and set the document – the equivalent of an Excel worksheet.

Set wd = CreateObject("Word.Application")
Set doc = wd.documents.Open("Word file path")

Then You can copy the content of the document and store it in clipboard.

doc.Content.Copy

Now comes the standard part of sending the email with a small modification. Instead of .Body You need to go to the WordEditor in your e-message.

Set editor = .GetInspector.WordEditor

After that just paste the text stored in clipboard by pasting that into the content of the editor object.

editor.Content.Paste

To this moment everything is easy and clear.

There goes the tricky part

Unfortunately using .Send method the content of the email body is empty. It appears that copy-pasting method is working only for .Display email method.

But nobody said that You can’t combine those 2 methods together.

.Display
.Send

It is causing a small flicker – just before sending, the email is popping up for a split second, but this is the only way to make it work.

Code

Option Explicit

Sub WordDocAsBody()

 Dim OutApp As Object, OutMail As Object
 Dim wd As Object, doc As Object, editor As Object

 Set wd = CreateObject("Word.Application")
 Set doc = wd.documents.Open("Word file path")
 doc.Content.Copy

 Set OutApp = CreateObject("Outlook.Application")
 Set OutMail = OutApp.CreateItem(0)

 With OutMail
     .To = "email address"
     .Subject = "subject"
     Set editor = .GetInspector.WordEditor
     editor.Content.Paste
     .Display
     .Send
 End With

 doc.Close 0
 Set OutMail = Nothing
 Set wd = Nothing
 Set OutApp = Nothing

 MsgBox "Done"

End Sub

That was the easiest way to have formatted text in the email body, the same way as in Word document. Although this has a small flicker, but I think we can consider this as the fastest and most comfortable way to send complex message using Outlook.

Author: Tomasz Płociński

I'm very advanced in VBA, Excel, also easily linking VBA with other Office applications (e.g. PowerPoint) and external applications (e.g. SAP). I take part also in RPA processes (WebQuery, DataCache, IBM Access Client Solutions) where I can also use my SQL basic skillset. I'm trying now to widen my knowledge into TypeScript/JavaScript direction.

15 thoughts on “Send Word document as email body”

  1. On ‘Set editor = .GetInspector.WordEditor’ I get “Run-time error ‘287’: Application-defined or object-defined error”.

    I have Office, Word, Excel and Outlook 16.0 Object Library references selected.

    Any suggestions?

    1. Is Word the default email editor?

      From the Inspector.WordEditor doc:
      The WordEditor property is only valid if the IsWordMail method returns True and the EditorType property is olEditorWord . The returned WordDocument object provides access to most of the Word object model…

  2. Thanks, quite simple and useful code!

    Any idea why it adds 1 line spacing when copied to the Outlook email? Looks good and no spacing on the Word document, but it appears with 1 empty row between each line of the text on the Outlook message.

    1. Hello Nikola!
      Have You checked your Word doc for these marks?
      I mean are You sure they are not 100% there, but only misformat whole text in new email?
      You can try to replace every double new lines for 1 empty line if there is still a problem.

  3. I work from two different PCs and keep the Word file I’d like to reference on Onedrive. Is there a way that I can use a URL to reference the Word doc?

    1. Have You tried to just use the Word doc from Onedrive the same way as it is stored on PC?

  4. If you use SharePoint you can host the file there to be accessed from multiple locations/pcs. Thats what I did for my team at work. I have an excel file that lets me choose which word template I want to send and it will open the document copy the content and paste it into a new Outlook email.

  5. I am having a slight issue. Sometimes it will not copy the word content into the email and I have to close the email and run the macro again. 9 times out of 10 it works the second time. Other than that this macro works so well!!

    Do you think there is a way to prevent this from happening?

    1. Make sure that You close every file (Word) You opened.
      Maybe try to give it a 1second break?
      Or loop until macro copy the content of Word.
      Probably it is the downside of opening those files from Sharepoint.

      1. It happens whether or not its on SharePoint.

        – I tried the 1 second break
        – I have made sure the files are closed

        Could you explain the loop until macro copies the content of Word?

        Still seems to “forgets” to copy to email randomly.

        1. You could try Do Until the email body contains the content of Word (any “specified word from Word content in email body) 🙂

          1. Thanks for the advice. Do you mind sharing what you mean?

            Maybe an example code? Here is what I am doing. Where do I put the loops?

            ——————————————————————-

            Set WordDoc = WordApp.Documents.Open(filename:=DocLoc, ReadOnly:=False) ‘Open Template

            WordDoc.Content.Copy

            Set OutApp = CreateObject(“Outlook.Application”) ‘Create Outlook Application
            Set OutMail = OutApp.CreateItem(0) ‘Create Email
            With OutMail
            .SentOnBehalfOfName = “Do not Send”
            .BCC = “Testing – Do not Send”
            .subject = “Testing 123”
            .Attachments.Add

            Set editor = .GetInspector.WordEditor
            editor.Content.Paste
            Application.CutCopyMode = False ‘Clears the Clipboard

            .Display ‘To send without Displaying change .Display to .Send

            ———————————————————————

            Im not sure if the issue is in the copy from Word or the paste to Outlook… Could be either. I think its the paste form outlook.

          2. Try to loop the part, where You getting the content of Word and trying to paste. Check if the body of email contains any of “gold” phrase of Word content.

  6. Could not get Loop to work.

    But……….

    I did find a work around. Before my email creation code I added an extra step of opening a new email, pasting content then closing email. This basically clears the error every time before it starts.

    ———————————

    Set OutApp = CreateObject(“Outlook.Application”) ‘Create Outlook Application
    OutApp.Visible = False ‘Make the application not visible to the user
    Set OutMail = OutApp.CreateItem(0) ‘Create Email
    With OutMail
    Set EmailTemplate = OutMail.GetInspector.WordEditor
    EmailTemplate.Content.Paste
    End With
    OutMail.Quit
    OutApp.Visible = True ‘Make the application visible to the user

Leave a Reply

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