0

Windows 10
Microsoft 365
Outlook v.2205


I am writing a macro that prints the selected email AND attachments to PDF. I need this to be fully automatic, without any user intervention once the macro starts. The problem I have, is that when the mailItem.PrintOut command is used, a print dialogue window appears and freezes the macro until the user advances the window manually.

I am able to cobble together the body of the email using the Word object library where I can easily print, but for email attachments (such as PDFs) I am at a loss.

I have scoured the web looking for a solution to this. I have found nothing so far that can be done in pure native VBA.

Image of the printer dialogue that I would like to bypass: enter image description here

I previously asked here whether it is possible to manipulate the printer dialogue window using API/VBA code, but later realized that once the printer dialogue appears, VBA stops working so I must find a way to bypass the dialogue window altogether.

It seems unlikely to me that there does not exist a way to do this.

Code posted below (simplified for reference):

Private Sub printEmail()

    Dim mySelection As Outlook.Selection
    Dim myEmail As MailItem

    Set mySelection = Application.ActiveExplorer.Selection

    ' If item = mailitem, print
    If mySelection.Item(1).Class = 43 Then
        Set myEmail = mySelection.Item(1)
        
        
        myEmail.PrintOut ' <===###this command causes a dialogue to appear###
    
    
    
    ' Else, exit sub
    Else
        MsgBox "Select a mail item"
        Exit Sub
    End If

End Sub
  • https://stackoverflow.com/a/70347971/11683? https://stackoverflow.com/a/31412480/11683? – GSerg Jul 25 '22 at 18:56
  • I wish I could use the SaveAs method, but unfortunately my company administrators block this feature of Outlook. As for the WordEditor method, I have been able to achieve a similar approach by invoking the Word Object library and creating an instance of Word and printing the email from there. The problem is that I need to be able to print attachments as well (PDFs mainly). I will update my question with this info. – Logan Price Jul 25 '22 at 19:03
  • You can use a low-level API which also provides such functionality outside of Outlook. Consider using Extended MAPI or any other third-party wrapper around that API such as Redemption (see `SaveAsFile` there). – Eugene Astafiev Jul 25 '22 at 20:34

1 Answers1

0

You need to save attached files (for example, PDF) on the disk and then use a shell command to print the saved file. Or if you have got Acrobat Reader installed on the system you could use something like that:

Dim oShell As Object, oExec As Object
Set oShell = CreateObject("Wscript.Shell")
Set oExec = oShell.exec("""" & "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" _
& """/p /h """ & SomePath & "\" & "invoice.pdf" & """")
DoEvents
oExec.Terminate
Set oExec = Nothing
Set oShell = Nothing
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • 1
    A shell command to print the saved file can do [whatever](https://stackoverflow.com/questions/61933672/printing-file-using-c-sharp-at-server?noredirect=1&lq=1#comment109541970_61933672) the handling application thinks is a good idea. Which may very well display an interface. – GSerg Jul 25 '22 at 22:04