Solved

password protect a pdf document in msaccess vba

Posted on 2014-11-13
9
765 Views
Last Modified: 2014-11-19
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
Tx
Alan
0
Comment
Question by:caandal
  • 5
  • 4
9 Comments
 
LVL 26

Expert Comment

by:MacroShadow
ID: 40441930
You will have to use third party pdf generation software, Microsoft doesn't provide an interface for password protecting.
0
 

Author Comment

by:caandal
ID: 40441946
I thought as much - do you have any suggestions - the stuff that I have looked at is pretty expensive
0
 
LVL 26

Expert Comment

by:MacroShadow
ID: 40441971
I use bullzip which is free, I'm putting the code together now.
0
 

Author Comment

by:caandal
ID: 40441974
Beaut tx
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 26

Accepted Solution

by:
MacroShadow earned 500 total points
ID: 40442329
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 & "\"
    Else
        sDirectory = sDirectory
    End If

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

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

    With oBullzipPDF
        .Init
        .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

    DoEvents
    DoCmd.OpenReport rptName, acViewNormal, , sFilterCriteria
    DoEvents

    If blnPrinterChanged Then SetDefaultPrinter strDefaultPrinter

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

End Function

Open in new window

0
 

Author Closing Comment

by:caandal
ID: 40442582
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
0
 
LVL 26

Expert Comment

by:MacroShadow
ID: 40444569
You're welcome :) Let us now how it goes.
0
 

Author Comment

by:caandal
ID: 40452194
MacroShadow
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.
Cheers
Alan
0
 
LVL 26

Expert Comment

by:MacroShadow
ID: 40452200
Good to hear.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now