• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1217
  • Last Modified:

password protect a pdf document in msaccess vba

Hi Experts

Has anyone found a way to password protect a pdf document in VBA.  Currently I am creating pdf's with the output to method but we now have a requirement for higher security.  

Any assistance will be much appreciated
  • 5
  • 4
1 Solution
You will have to use third party pdf generation software, Microsoft doesn't provide an interface for password protecting.
caandalAuthor Commented:
I thought as much - do you have any suggestions - the stuff that I have looked at is pretty expensive
I use bullzip which is free, I'm putting the code together now.
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

caandalAuthor Commented:
Beaut tx
Here is a function you can use, it doesn't require adding references but you must install Bullzip PDF Printer

There are several security settings you can set, see the code for more info, their names are fairly self explanatory.
The function accepts four string arguments: the report to print, report filter criteria, directory to save to and the name to give the file. Only the first argument is required the rest are optional.

One more note, Bullzip PDF Printer is only free for less than 11 users.

Now to the code:
Option Explicit

Public Declare Function SetDefaultPrinter Lib "winspool.drv" _
                                          Alias "SetDefaultPrinterA" (ByVal pszPrinter As String) As Long

Function PrintReportAsPDFwithBullZip(ByVal rptName As String, _
                                      Optional sFilterCriteria As String = "", _
                                      Optional sDirectory As String = "", _
                                      Optional sFileName As String = "") _
                                      As Boolean

    On Error GoTo err_Error

    Dim oBullzipPDF As Object, oBullzipUtil As Object
    Dim strSavePath As String, strFileName As String
    Dim strDefaultPrinter As String
    Dim blnPrinterChanged As Boolean

    Set oBullzipPDF = CreateObject("Bullzip.PDFPrinterSettings")   'Initialize the PDF class

    'set the success flag to true here but it will be set to
    'false if the function fails at any point
    PrintReportAsPDFwithBullZip = True

    If sDirectory = "" Then
        sDirectory = CurrentProject.Path & "\"
        sDirectory = sDirectory
    End If

    If sFileName = "" Then
        sFileName = Split(CurrentProject.Path, "\")(UBound(Split(CurrentProject.Path, "\")))
        sFileName = sFileName
    End If

    If LCase(Right(sFileName, 4)) <> ".pdf" Then
        sFileName = sFileName & ".pdf"
    End If

    With oBullzipPDF
        .SetValue "Output", sDirectory & sFileName
        .SetValue "ShowSettings", "never"

        ' Here you have to set the desired security settings
        .SetValue "OwnerPassword", "123"
        .SetValue "UserPassword", "123"
        .SetValue "EncryptionType", "Standard128bit"    ' AES 128 bit and AES 256 bit encryption are supported but you must purchase a license to use it
        .SetValue "AllowAssembly", "True"
        .SetValue "AllowCopy", "True"
        .SetValue "AllowDegradedPrinting", "True"
        .SetValue "AllowFillIn", "True"
        .SetValue "AllowModifyAnnotations", "True"
        .SetValue "AllowModifyContents", "True"
        .SetValue "AllowPrinting", "True"
        .SetValue "AllowScreenReaders", "True"
        ' End of security settings

        .SetValue "ShowPDF", "no"
        .SetValue "ConfirmOverwrite", "no"
        .SetValue "SuppressErrors", "yes"
        .SetValue "ShowProgress", "no"
        .SetValue "ShowProgressFinished", "no"
        .SetValue "Author", "Me"
        .SetValue "Title", "My File"
        .SetValue "Subject", "My Subject"
        .WriteSettings (True)    'writes the settings in a runonce.ini that is immediately deleted after being used.
    End With

    If InStr(Application.Printer.DeviceName, "BullZip") = 0 Then    ' If BullZip isn't the default printer
        blnPrinterChanged = True                                    ' Set the printer changed flag to true
        strDefaultPrinter = Application.Printer.DeviceName          ' Save name of current printer
        SetDefaultPrinter "Bullzip PDF Printer"                     ' Use API to set the Current printer to Bullzip
    End If

    DoCmd.OpenReport rptName, acViewNormal, , sFilterCriteria

    If blnPrinterChanged Then SetDefaultPrinter strDefaultPrinter

    'error handler and exit
    Set oBullzipPDF = Nothing
    Exit Function
    PrintReportAsPDFwithBullZip = False
    MsgBox Err.Description
    Resume err_Exit

End Function

Open in new window

caandalAuthor Commented:
Hi Macroshadow

You are a star many thanks.  I have not tested it yet but looking though the code it all makes sense.  I am familiar with printer manipulation before printing as well.  This is a lot more simple than some of the other solutions that I looked at - and Bullzip is free - bonus

Thanks again
You're welcome :) Let us now how it goes.
caandalAuthor Commented:
It works flawlessly many thanks.  The client is exceedingly chuffed because the pdf software that they were using now is asking for a subscription so we have killed two birds with one stone.  The bonus is because theres are only 8 staff members using it they do not have to pay for the commercial license.
Good to hear.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now