Outlook 2010 - Use VBA to send PDF file as body rather than attachment

MonteDelMar
MonteDelMar used Ask the Experts™
on
Hi Experts

Is it possible to use VBA to insert a PDF file as the body of the email (as though using Insert, Object etc)?  Currently I create the file in Word and have VBA code (also in Word) to convert it to a PDF and attach it to that email but would like to make it the body rather than an attachment.

Many thanks
Archie
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2014

Commented:
Don't bother with the PDF.
Save the Word document out as HTML to a file location
You can then open the HTML as a textstream and blow it into the .HTMLBody of the email.

With objOutlookMsg
            .BodyFormat = olFormatHTML
            Dim ts As Object
            Dim fs as Object
            Set fs = CreateObject("Scripting.FileSystemObject")
            If fs.FileExists("c:\tempPDF\Summary.htm") = False Then
                GoTo skipHTML
            End If
            Set ts = fs.GetFile("c:\tempPDF\Summary.htm").OpenAsTextStream(1, -2)
            .HTMLBody = "<p>This is an automated sending by me as requested. </p>Please respond to myname@somewhere.com with any inquiries.</P><br><br>"
            .HTMLBody = .HTMLBody & ts.ReadAll
End with

skipHTML:

Open in new window


I do this with Excel to get a formatted table into the body of an email and it works very nicely

Author

Commented:
Thanks for your reply, Nick.

The original document contains headers and footers with all sorts of info plus some graphics.  It also contains a logo font which the recipient won't have access to.  This is partly why it is created as a PDF.  Is it possible to achieve this when sending as HTML?
Most Valuable Expert 2014

Commented:
The only way to know will be to save the document as HTML and look at it.
How it renders as HTML will be how it renders in the body of the email message.

Currently I create the file in Word

If it doesn't render nicely as HTML, you might need to adjust the creation until it does.
PDF is a very nasty format to tackle from VBA.
MS Word can be the email editor for Outlook 2010
It may be possible to write directly to the mail message in Word native format.
In that case, the PDF part is a red herring :)

Look here
https://msdn.microsoft.com/en-us/library/dd492012(v=office.12).aspx#Outlook2007ProgrammingCh17_UsingWordEditor
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
It might help if I explain a bit more about how we're using this.  We create a letter in Word and then, if it is to be sent in an email, add letterhead details (header and footer), save as a pdf and attach it to an email. When the recipient open the attachment, it looks like a printed letter would do and they can't modify the content.  There are 5 or 6 different letterheads.
Most Valuable Expert 2014
Commented:
As far as I know, you aren't going to be able to create a read-only mail message body.
And trying to finalize Word to PDF and then pull that back to a mail message body is both a pain-in-the-neck, at best, or close to impossible without Adobe Acrobat installed.

Since you are working with Word already, the best approach is the one lined out in the link I posted.
Instead of working with Word and a Word document, you work with Word-as-editor and your document becomes the mail message

   Dim objOL As Outlook.Application
    Dim objNS As Outlook.NameSpace
    Dim objDoc As Word.Document
    Dim objSel As Word.Selection
    Dim strStamp As String
    On Error Resume Next
    Set objOL = Application
    If objOL.ActiveInspector.EditorType = olEditorWord Then
        Set objDoc = objOL.ActiveInspector.WordEditor
        Set objNS = objOL.Session
        strStamp = "A line of text we want to add to the body"
        Set objSel = objDoc.Windows(1).Selection
        objSel.Move wdStory, -1
        objDoc.Characters(1).InsertBefore _
          strStamp & vbCrLf & vbCrLf
        objSel.Move wdParagraph, 1
    End If
    Set objOL = Nothing
    Set objNS = Nothing

At that point, everything you are presently doing with Word VBA can be done to the mail message.

But it certainly won't be read-only!

Author

Commented:
Great.  Thanks for your help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial