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.
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
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
Do we need the pro version for the code to work?
ReplyDeleteNo, I didn't paid for a pro version. It worked with the free version of PDFtk.
DeleteGREAT!!! Works with the free version.
ReplyDeleteCode is stopping at Shell cmdStr, vbHide 'Executing PDFtk Command.
ReplyDeleteCould you help?
Are you implementing all the steps correctly ??
DeleteIf yes, it should work.
Hello:
ReplyDeleteI 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
You told nothing about Error.
Delete1. 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,
Hi Vikas
ReplyDeleteI have an error. line cmdStr = "pdftk" ...
VBA have a mess: cmdStr is not define