5
votes

I am attempting to save all of the mail items within a folder in Outlook as PDF.

Sub pdfConversion()
    Dim outApp As Object, objOutlook As Object, objFolder As Object, myItems As Object, myItem As Object
    Dim psName As String, pdfName As String

    Set outApp = CreateObject("Outlook.Application")
    Set objOutlook = outApp.GetNamespace("MAPI")
    Set objFolder = objOutlook.GetDefaultFolder(olFolderInbox).Folders("PDF Conversion")
    Set myItems = objFolder.Items

    For Each myItem In myItems
        myItem.PrintOut copies:=1, preview:=False, ActivePrinter:="Adobe PDF", printtofile:=True, _
        collate:=True, prtofilename:="C:\Users\lturner\Documents\" & myItem.Subject & ".pdf"
    Next myItem
End Sub

I am using Outlook 2007, which doesn't have the option to save mails as PDF, hence I'm attempting to use the .PrintOut method.

Using the above I am currently receiving a "Named argument not found" error. I've looked elsewhere on the internet, but cannot seem to find a solution.

4

4 Answers

5
votes

I used a combination of the answers posted by Krishna and Eugene Astafiev to produce the below code, which will now produce a PDF document out of the myItem.

Dim objDoc As Object, objInspector As Object
For Each myItem In myItems
    fileName = Replace(myItem.Subject, ":", "")
    Set objInspector = myItem.GetInspector
    Set objDoc = objInspector.WordEditor
    objDoc.ExportAsFixedFormat folderPath & fileName & ".pdf", 17
    Set objInspector = Nothing
    Set objDoc = Nothing
Next myItem

Posting this so anyone in the future who stumbles across the question can see the working code, which uses the WordEditor property.

2
votes

There is no need to use the SaveAs method of the MailItem class. The WordEditor property of the Inspector class returns an instance of the Word Document class which represents the message body. You can call the ExportAsFixedFormat method of the Document class directly from Outlook avoiding any disk operations. See Chapter 17: Working with Item Bodies for more information.

1
votes

MailItem.Printout doesnt take any paramters. it uses application level default settings.

The workaround i can think here is 2 step,

Step 1 use MailItem.SaveAs "somefile.doc", olDoc to convert to a RTF or Document

Step 2 using word automation, (reference word objects 14.0) convert this to pdf using fixedformat.

Document.ExportAsFixedFormat "somefile.pdf", 17
0
votes

This will only work if the original email is formatted as document that supports word conversion.

For instance if you are trying to print a Custom Form to PDF then this will fail because the Inspector will always pass back NOTHING for the word editor.