Solved

password protect a pdf document in msaccess vba

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

Author Comment

by:caandal
ID: 40441974
Beaut tx
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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.
This article will show, step by step, how to integrate R code into a R Sweave document
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

911 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

18 Experts available now in Live!

Get 1:1 Help Now