Solved

password protect a pdf document in msaccess vba

Posted on 2014-11-13
9
892 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 27

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 27

Expert Comment

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

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

Author Comment

by:caandal
ID: 40441974
Beaut tx
0
 
LVL 27

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 27

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 27

Expert Comment

by:MacroShadow
ID: 40452200
Good to hear.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
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…

809 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