Print protected pdf attachment in an email in outlook

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Print protected pdf attachment in an email in outlook

Post by YasserKhalil »

Hello everyone
I have this VBA code that enables me to print PDF attachment using Outlook and it works fine

Code: Select all

'Reference : Tools >> Reference >> Adobe Acrobat 10.0 Type Library

Private Declare PtrSafe 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

Sub PrintAttachmentsSelectedMsg()
    Dim oMail       As Outlook.MailItem
    Dim obj         As Object
    Dim colAtts     As Outlook.Attachments
    Dim oAtt        As Outlook.Attachment
    Dim sFile       As String
    Dim sDirectory  As String
    Dim sFileType   As String

    For Each obj In ActiveExplorer.Selection
        Set oMail = obj
        
        'CHANGE TO SUIT
        sDirectory = "D:\Attachments\"
        Set colAtts = oMail.Attachments

        If colAtts.Count Then
            For Each oAtt In colAtts
                sFileType = LCase$(Right$(oAtt.FileName, 4))

                Select Case sFileType
                    Case ".pdf"
                        sFile = sDirectory & oAtt.FileName
                        oAtt.SaveAsFile sFile
                        AcrobatPrint sFile, "All"
                End Select
            Next oAtt
        End If
    Next obj
End Sub

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")

    AcroExchAVDoc.Open FileName, ""
    Set AcroExchPDDoc = AcroExchAVDoc.GetPDDoc

    num = AcroExchPDDoc.GetNumPages - 1

    If PrintMode = "All" Then
        Call AcroExchAVDoc.PrintPages(0, num, 2, 1, 1)
    Else
        If num = 0 Then
            Call AcroExchAVDoc.PrintPages(0, num, 2, 1, 1)
        Else
            Call AcroExchAVDoc.PrintPages(0, 1, 2, 1, 1)
        End If
    End If

    AcroExchApp.Exit
    AcroExchAVDoc.Close (True)
    AcroExchPDDoc.Close
End Sub
How can it be edited to print a protected pdf file (I mean to code the password) and open it for printing job

Thanks advanced for help

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Print protected pdf attachment in an email in outlook

Post by HansV »

Adobe doesn't provide a way to open a password-protected PDF file. You could try SendKeys:

Code: Select all

    SendKeys "MyPassword{Enter}"
    AcroExchAVDoc.Open FileName, ""
I cannot test this code myself.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Re: Print protected pdf attachment in an email in outlook

Post by YasserKhalil »

Thanks a lot for reply
As for Sendkeys is not guaranteed. Is there a way to do the task without depending on Adobe acrobat pro?
I searched and found some scripts but didn't work for me. And I can't figure it out

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Print protected pdf attachment in an email in outlook

Post by HansV »

I don't know of an alternative. If you want to open a PDF file from code, it's best not to use a password.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Re: Print protected pdf attachment in an email in outlook

Post by YasserKhalil »

Thanks for reply
As for the sender he would prefer putting password for PDF ..
Generally is there a way to print pdf file without relying on the Acrobat Pro?

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Print protected pdf attachment in an email in outlook

Post by HansV »

In Windows 10, you can view and print PDF files without needing a third-party utility.
In older versions, you do need a PDF viewer or editor. It doesn't have to be Adobe Acrobat. Adobe Reader is sufficient, but there are alternatives: Foxit Reader, Sumatra PDF, ...
As long as a default PDF viewer is available, you can print a PDF file using ShellExecute.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Re: Print protected pdf attachment in an email in outlook

Post by YasserKhalil »

I already found some codes that have ShellExecute but doesn't work for me

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Print protected pdf attachment in an email in outlook

Post by HansV »

I'm afraid you're out of luck then...
Best wishes,
Hans