Skip to main content

Making Password Protected PDFs using Excel Vba and PDFtk Tool

Hi Everyone,
This time, I have come up a VBA Code to generate Password protected PDFs using Excel. Actually, Excel Vba has .ExportAsFixedFormat Method to generate PDFs but this hasn't any Parameter which takes password to protect the PDFs. So I have used PDFtk Tool which provide Command Line Interface to make PDFs protected using Password. Actually, You can do various things using PDFtk Tool command line varying from creating, merging, Protecting and many other. So Download the PDFtk Tool from the following link:-


Here are the Steps:-
1. Install the PDFtk Toolkit.
2. Use the following code to Print or Export the Activesheet with a password.



       
Sub PasswordProtectedPDFs()

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    Dim fTemp As String                                   'Defining Variables
    Dim oPdf As String
    Dim Pwd As String
    
    fTemp = ThisWorkbook.Path & "\" & "Temp.Pdf"          'Full path for temporary file.
    oPdf = "D:\ProtectedPDF.Pdf"                          'Set Path and Name for Protected Output PDF here.
    Pwd = "vikas"                                         'Set appropriate Password here.
    
    With ActiveSheet                                      'Making a Temporary Unprotected Pdf file.
       .ExportAsFixedFormat Type:=xlTypePDF, _
                            Filename:=fTemp, _
                            Quality:=xlQualityStandard
    End With
    
    fTemp = """" & fTemp & """"                           'Putting extra "" around for command Parameter.
    oPdf = """" & oPdf & """"
    Pwd = """" & Pwd & """"
                                                          'Making Command String for making protected PDFs Using PDFtk tool.
    cmdStr = "pdftk " & fTemp _
                      & " Output " & oPdf _
                      & " User_pw " & Pwd _
                      & " Allow AllFeatures"

    Shell cmdStr, vbHide                                  'Executing PDFtk Command.
    
    Application.Wait DateAdd("s", 2, Now)                 'Allowing 2 secs for command to execute.
    
    Kill Replace(fTemp, """", "")                         'Deleting temporary files.
    
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    
    MsgBox "Finished", vbInformation
    
End Sub 

Here is the link where you can see its application:-
http://www.excelforum.com/excel-programming-vba-macros/1069367-making-password-protected-pdfs-using-excel-vba-and-pdftk-tool.html

Remarks:-
You can do many more things using excel vba and PDFtk tool Command line interface. Here is the link where you can see many PDFtk functions and its command line parameters.
PDFtk Command Line Manual

Regards,
Vikas Gautam

Comments

  1. Do we need the pro version for the code to work?

    ReplyDelete
    Replies
    1. No, I didn't paid for a pro version. It worked with the free version of PDFtk.

      Delete
  2. GREAT!!! Works with the free version.

    ReplyDelete
  3. Code is stopping at Shell cmdStr, vbHide 'Executing PDFtk Command.
    Could you help?

    ReplyDelete
    Replies
    1. Are you implementing all the steps correctly ??
      If yes, it should work.

      Delete
  4. Hello:
    I am using your above method to create PW protected pdf but it errors out at : .ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:=fTemp, _
    Quality:=xlQualityStandard

    Please let me know the solution
    Thanks

    ReplyDelete
    Replies
    1. You told nothing about Error.
      1. Please mention which error you are getting ?? Preferably a screenshot.
      2. Which version of excel you are using ??
      3. Are you able to Save As a file as PDF in your version of excel ??

      Regards,

      Delete
  5. Hi Vikas

    I have an error. line cmdStr = "pdftk" ...

    VBA have a mess: cmdStr is not define

    ReplyDelete

Post a Comment

Popular posts from this blog

Automation:- Sending Invitation to Meeting Using Excel VBA

Hello Everyone, In one of the previous post, I wrote about automating Sending Emails using Excel VBA. This time I have come up with a pretty similar code. The code below sends Outlook Meeting Invitations to recipients on one click. here is the Code:- Sub Send_Invite_Auto() Dim olApp As Outlook.Application Dim olApt As AppointmentItem Set olApp = New Outlook.Application 'Creating Outlook Session Set olApt = olApp.CreateItem(olAppointmentItem) 'Creating an Appointment With olApt .Subject = "Enter the subject here." 'Subject .Start = DateAdd("d", 5, Now) 'Enter Date + Time here. .Recipients.Add ("example@gmail.com") 'Recipient Name, Alias, or any other Attribute. .MeetingStatus = olMeeting 'olAppointmentItem with Meeting status olMeeting 'becom

Highlighting Duplicates across multiple sheets

Hi One and All, This time I have come up with some conditional formatting stuff. The aim is to highlight the duplicates across multiple sheets and with in the sheet as well. Assumptions:- 1. I am assuming that sheet names goes on like sheet1, sheet2, sheet3.... 2. The Target Column No. is same in all the sheets. I mean, as in the attached example, its Column A which is being targeted in both sheets.