I'm often asked how to control print settings when using a macro to print from Outlook. In most cases you can't, because the print dialog is not exposed in the object model. You can use Windows Shell to print any document type, using the native application, assuming you have an application installed that can open and print the file type, but you can't set options, such as page numbers, using Windows Shell.
If the application supports OLE, you may be able to control some aspects of the printout. For example, if you are printing a PDF and have Adobe Acrobat installed, you can use the PrintPages function in the Acrobat object model to print selected pages and shrink to fit the page.
Function PrintPages(nFirstPage As Long, nLastPage As Long, nPSLevel As Long, bBinaryOk As Long, bShrinkToFit As Long) As Boolean
Call the function in the macro using this format:
Call AcroExchAVDoc.PrintPages(0, 1, 2, 1, 1)
The page count begins with 0, not 1, so to print the first 3 pages, you'd use 0 and 2 as the first 2 values:
Call AcroExchAVDoc.PrintPages(0, 2, 2, 1, 1)
To print only page 2, use 1 and 1 as the first 2 values:
Call AcroExchAVDoc.PrintPages(1, 1, 2, 1, 1)
To use this code in Office applications, you need to set a reference to Acrobat in the VB Editor's Tools, References dialog box.
In the sample code below, pass the FileName and optional PrintMode values to the macro using
AcrobatPrint "c:\My Documents\my-file.pdf", "All"
AcrobatPrint "c:\My Documents\my-file.pdf"
Public Sub AcrobatPrint(FileName As String, PrintMode As String) Dim AcroExchApp As Acrobat.CAcroApp Dim AcroExchAVDoc As Acrobat.CAcroAVDoc Dim AcroExchPDDoc As Acrobat.CAcroPDDoc Dim num As Integer Set AcroExchApp = CreateObject("AcroExch.App") Set AcroExchAVDoc = CreateObject("AcroExch.AVDoc") ' Open the [Filename] pdf file AcroExchAVDoc.Open FileName, "" 'Get the PDDoc associated with the open AVDoc Set AcroExchPDDoc = AcroExchAVDoc.GetPDDoc 'Get the number of pages for this pdf num = AcroExchPDDoc.GetNumPages - 1 If PrintMode = "All" Then 'Print Entire Document Call AcroExchAVDoc.PrintPages(0, num, 1, 1, 1) Else If num = 0 Then 'Print one page document Call AcroExchAVDoc.PrintPages(0, num, 2, 1, 1) Else 'Print first two pages Call AcroExchAVDoc.PrintPages(0, 1, 1, 1, 1) End If End If AcroExchApp.Exit AcroExchAVDoc.Close (True) AcroExchPDDoc.Close End Sub
Developing Applications Using Interapplication Communication
Starting with version 7, the third parameter of printPages() (PSLevel) must be set to 2 or 3.
1 seems not to be allowed anymore.
I have not had success with this method. The line that should print the PDF does not print, with no error messages. I tried it on two computers, both running Acrobat XI Standard, and I also tried using the old SendKeys method, without success. Do you have any suggestions? I tried to paste my code, but it is too long. I am running 64-bit Windows 10, and my code runs from Access 2010 (with a reference to the Outlook and Acrobat object models). The code saves PDF attachments from Outlook mail messages and then attempts to print them using your… Read more »
Hi, this was very useful. Well it would if I could actually obtain a printed copy 🙂
So a few questions associated with it:
How do I state the printer to use? Must it be the default?
How come nothing actually comes out of my default printer ... or indeed any printer ?
Thanks for any help you can give.
This macro is actually creating a pdf, not sending one to a paper printer. But... when you are printing to paper from a macro, you would need to call up windows api to change the printer (i don't have any code samples for that). To use the default printer, declare Private Declare Function ShellExecute Lib "shell32.dll" Alias _ "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _ ByVal lpFile As String, ByVal lpParameters As String, _ ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long then use ShellExecute 0, "print", sFile, vbNullString, vbNullString, 0 in your macro. (a… Read more »