none
Looking for VBA code to attach the contents of an HTML document to an e-mail (like "Insert as text") RRS feed

  • Question

  • Using the following "manual" steps I can achieve my aim: to create an e-mail with contents from a Word document.

    Step 1 - Using word 2010 I create a single-page document that contains text, tables, formatting and a (.jpg) picture.

    Step 2 - I Save the document using File, SaveAs - SaveAs Type 'Web Page (*.htm; *.html)'.

    Step 3 - Using Outlook 2010 I create a new e-mail (type HTML)

    Step 4 - On the Message ribbon, in the Include group, I click Attach File;

    Step 5 - On the 'Insert File' form I select the .htm file which I created in Step 2 above

    Step 6 - On the 'Insert File' form I select the 'Insert as text' option on the Insert button

    Step 7 - I send the e-mail

    N.B. I can automate all of the above steps using VBA in Word 2010 except Step 6.  

    Because there is no Macro Recorder in Outlook I have no way of finding VBA "clues" and hours of searching the Internet have proved unfruitful.

    I am sure that someone in our Developer community will have already solved this conundrum.

    All and any assistance well be warmly welcomed.

    Rgds,

    Paul J

    Wednesday, November 20, 2013 4:48 PM

All replies

  • Hi Paul,

    According to your description, you want to attach the contents of an HTML document to a message body.  As far as I know, there is no such API in Outlook Object Model you can simulate the action you described in step 6. However you can refer code below to set the HTMlBodby property for a MailItem:

    Sub CreateHTMLMail() 
     
     'Creates a new e-mail item and modifies its properties. 
     
     Dim objMail As Outlook.MailItem 
     
     'Create e-mail item 
     
     Set objMail = Application.CreateItem(olMailItem) 
     
     With objMail 
     
     'Set body format to HTML 
     
     .BodyFormat = olFormatHTML 
     
     .HTMLBody = _ 
     
     "<HTML><BODY>Enter the message text here. </BODY></HTML>" 
     
     .Display 
     
     End With 
     
    End Sub

    Please refer to link below for more details:
    MailItem.HTMLBody Property (Outlook)

    Then another workable way you can send the Word document as message content easily, you can use Document.MailEnvelope. Below is a simple sample for your reference:

    Sub sendDocument()
    With Application.ActiveDocument.MailEnvelope.Item
    .To = "Person1@microsoft.com"
    .Subject = "My Subject"
    .send
    End With
    End Sub

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, November 21, 2013 12:39 PM
    Moderator
  • Hi Fei,

    Thanks for your swift response.

    Your first suggestion in the closest to what I am looking for:  I have tried something very similar (by reading an HTM file into a single String object) - and the text and its formatting shows correctly in the BodyText area of the e-mail BUT the .jpg picture does not.

    Manually executing the 7 steps outlined above, starting at the Outlook home screen results in a fully viable e-mail which arrives at the recipient's mail-box with text, tables, formatting and pictures intact.

    All I want to do is reproduce these manual steps in a Word VBA macro - but I am unable to replicate step 6 - and I have been unable to find anyone who can.

    If there are any experienced Microsoft Outlook VBA developers out there perhaps you can help, please?

    Rgds,

    Paul J

    Saturday, November 23, 2013 4:25 PM