PDA

View Full Version : Solved: password protect document printing



General_Kaos
11-27-2009, 04:21 AM
Hi Guys,

As part of a document control system I needed to write a macro to prevent unauthorised printing of word documents. This seemed simple and I wrote the following:

Sub fileprint() 'capture fileŠPrint action and requires password
Dim Message, Title, Default, pass
Message = "Enter password to print" ' Set prompt.
Title = "Password?" ' Set title.
Default = "" ' Set default.
' Display message, title, and default value.
pass = InputBox(Message, Title, Default)

If pass = "mypassword" Then
ActiveDocument.PrintOut
Else
MsgBox ("Wrong Password, Printing not allowed!")
End If
End Sub

CommandBars("Standard").Controls("Print").OnAction = "fileprint" 'directs use of the print button to the print macro


This works nicely within word, but doesn't prevent printing via document context menu in windows explorer.

Can anyone point me in the right direction for capturing this command and redirecting it?

Thanks,

Lex :-)

macropod
11-29-2009, 03:25 PM
Hi Lex,

There's really nothing you can do with vba to prevent the unauthorised printing of a document. All one has to do to break your protection is to disable the document's macros upon opening - and you can't prevent that.

General_Kaos
11-30-2009, 01:17 AM
Hi Macropod,

Thanks for your response.

We can control it to a certain extent as the system will only be used on PCs within the company.

The project is signed, allowing macro security to be high, and access to the tools/macros menu and atl/F11 VBE is blocked.

The system doesn't really have to be totally secure (if someone really wanted to they could just type the document out!) but I do want to stop the obvious ways of printing a document, and the only one I haven't managed to capture is the right click from windows explorer.

Thanks for your thoughts,

Lex

fumei
11-30-2009, 01:08 PM
I believe the only way is through the Local Group Policy.

User Configuration, Administrative Templates, Windows Components, and Windows Explorer.

This is a MMC snap-in.

If you are using Windows 7, there are some downloads to cover this at:

http://www.sevenforums.com/tutorials/5458-windows-explorer-context-menu-enable-disable.html

General_Kaos
12-01-2009, 01:44 AM
Hi Fumei,

Thanks for your response.

I am using XP on most of the systems and your solution works nicely.

(Microsoft KB 307882 for using MMC)

It does remove the entire context menu - but I can live with that!

I had hoped the solution would come from whatever Word macro is used by Windows to do the printing, but c'est la vie!

Thanks again for your help,

Lex

fumei
12-01-2009, 12:32 PM
"I had hoped the solution would come from whatever Word macro is used by Windows to do the printing, but c'est la vie!"

Except when you are using Windows Explorer to print, no Word macro, or command, is used. That is why you can use (in Windows Explorer) a right-click "Print" on any printable file.

General_Kaos
12-02-2009, 02:02 AM
Hi Guys,

I've managed to stop it by using the DocumentBeforePrint event:

Under this document:


Dim Current_Document As New class1


Private Sub Document_Open()
Register_Event_Handler 'run register_event_handeler when doc opens to allow use of class
End Sub

Sub Register_Event_Handler()
Set Current_Document.appWord = Word.Application
End Sub

And in a class module called class1
Public WithEvents appWord As Word.Application

Private Sub appWord_DocumentBeforePrint _
(ByVal Doc As Document, _
Cancel As Boolean)

Dim intResponse As Integer

intResponse = MsgBox("Printing Not Allowed! " _
& "Please Contact QA for authorised copy", _
vbOK)

If intResponse = vbOK Then Cancel = True
If intResponse <> vbOK Then Cancel = True
End Sub

Somewhat scrappy but works.

Thanks for all your help,

Lex

fumei
12-03-2009, 12:00 PM
This seems odd:
If intResponse = vbOK Then Cancel = True
If intResponse <> vbOK Then Cancel = True
In other words...no matter WHAT intResponse is (equal to vbOK and not equal to vbOK), Cancel = True.

In which case:
Public WithEvents appWord As Word.Application

Private Sub appWord_DocumentBeforePrint _
(ByVal Doc As Document, _
Cancel As Boolean)

MsgBox "Printing Not Allowed! " _
& "Please Contact QA for authorised copy"

Cancel = True
End Sub
BTW: VBA converts all Integer to Long automatically, so you may as well just use Long, not Integer.

General_Kaos
12-04-2009, 04:34 AM
Hi Fumei,

I just grabbed some code and added the second line to stop it printing if you clicked cancel!

Yours is more elegant!

I'm going to put a password bit in there anyhow...

Thanks again for your help,

Lex