Excel VBA help

Associate
Joined
25 Aug 2005
Posts
717
Location
Derbyshire
I am trying to restrict printing on an excel sheet I have. I have managed to do this by using VAB and the following code
Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)

Cancel = True
MsgBox ("If you require a hard copy of this sheet, please inform xxxxxxxx")

End Sub

Now, I want to be able to allow users to print if they enter the right value into a textbox in a userform and then press a "print" button. This is what I'm having problems with.
It will obviously have to involve an if statement along the line of:

Code:
If textbox1 = "value" then 
activeworksheet.print
Else cancel print

But I'm having problems with the correct VBA terminology for this.

Any help would be great, thanks
 
Never mind. I found a way. Just for anyone that is interested, I did it this way;
Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
'must lock VBA project so you can't see the password in it
'Input box to verify password before printing
'put in this workbook module
Dim myPassword As String

myPassword = InputBox(prompt:="Due to the sensitive nature of the material printing is restricted. Please obtain a password from xxxxxxx to proceed:", _
Title:="Password is required to print this file.")
'change password to what you want
If myPassword <> "xxxx" Then
MsgBox prompt:="Click OK to return to Report.", _
Title:="Cancelled -- correct password not entered", _
Buttons:=16
Cancel = True

Else
End If
End Sub
 
Back
Top Bottom