VBA Code to allow refrain certain users from printing the file

SharmaAntriksh

New Member
Joined
Nov 8, 2017
Messages
31
I am using the bellow event procedure of VBA and i want to restrict certain users from printing this file excluding me.

For eg - David, Carly and Rachel are not allowed to print the file but Jim, Antriksh, Grant and Rohit can print the file


Private Sub Workbook_BeforePrint(Cancel As Boolean)

MsgBox "You're Not allowed to print"
Cancel = True

End Sub

please let me know if there is a solution for this
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi,

Try this:-

Private Sub Workbook_BeforePrint(Cancel As Boolean)


If Application.UserName = "David" Or Application.UserName = "Carly" Or Application.UserName = "Rachel" Then
MsgBox ("You're Not allowed to print")
Cancel = True

End If


End Sub
 
Upvote 0
Another approach would be to give the approved users a password they would have to enter to allow printing. This macro would go into the code module for ThisWorkbook. Change the passwords in the code to suit your needs and add any additional ones you may need.
Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
    Dim mypassword As String
    mypassword = InputBox("Please enter your password.")
    Select Case mypassword
        Case "Jim's password", "Antriksh's password", "Grant's password", "Rohit's password"
           '
        Case Else
            MsgBox "Invalid password.  Please try again."
            Cancel = True
    End Select
End Sub
 
Last edited:
Upvote 0
Another option
Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)

    Dim Nme As String
    Dim Ary As Variant

    Ary = Array("[COLOR=#ff0000]David[/COLOR]", "[COLOR=#ff0000]Carly[/COLOR]", "[COLOR=#ff0000]Rachel[/COLOR]")
    Nme = Environ("Username")
    
    If UBound(Filter(Ary, Nme, False, vbTextCompare)) > 0 Then
        MsgBox ("You're Not allowed to print")
        Cancel = True
    End If

End Sub
Where the names in red need to match the users window login username.
Because this uses the windows user name rather then the application username, it should help to prevent people bypassing the procedure.
 
Upvote 0
Just realised there were a couple of probs with my previous code. Use this instead
Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)

    Dim Nme As Variant
    Dim Ary As Variant

    Ary = Array("David", "Carly", "Rachel")
    Nme = Environ("Username")

    If UBound(Filter(Ary, Nme, True, vbTextCompare)) >= 0 Then
        MsgBox ("You're Not allowed to print")
        Cancel = True
    End If

End Sub
Ubound returns the upper limit of the size of an array, & if the ubound is greater than or equal to 0 it means the username is in the array & therefore not allowed to print
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top