Prompt for Passsword if not on approved list using windows authentication

steve1262

Board Regular
Joined
Aug 19, 2006
Messages
126
I have been searching for this without any luck.

I am looking for a code that will look at a very hidden sheet called "Approved Users" for a list of approved names that = their windows authentication (or login user name). If it is found then the password prompt is bypassed and they are allowed into the workbook. If not found then the user has to know the password to have access to the workbook.


Not sure if this is even possible????

Thank you
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Re: Prompt for Passsword if not on approved list using windows authentication - NOT SOLVED

Ok I have this code and it works except I can not get to actually insert the password and open the work book. Or just bypass the prompt if the user name is on the list.

Code:
Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Code:
Private Sub Workbook_Open()
Dim UName As String * 255
Dim L As Long: L = 255
Dim Res As Long
    Res = GetUserName(UName, L)
    On Error GoTo notfound
    If Range("A1:A10").Find(What:=UName, MatchCase:=False, LookAt:=xlWhole).Select Then 'Change false to true if you want case sensetive
        Call FoundIt
        Exit Sub
    End If
notfound:
    If Err.Number = 91 Then 'This is done to allow other error messages to still report correctly
        MsgBox "Not Found"
    Else
        MsgBox "Error: " & Err.Number & " - " & Err.Description
    End If
 
End Sub

Code:
Sub FoundIt()

Dim strPassword As String
 
strPassword = "1"
 
 'protect/unprotect workbook

ActiveWorkbook.Unprotect strPassword

End Sub

Any ideas???
 
Upvote 0
The workbook_open event code won't run until the password is entered and the workbook is actually opened ..

One thing you can do is have the hidden sheet and workbook_open code in an intermediary unprotected workbook .. If the user name is on the list then open the actual workbook from it and self-close the intermediary workbook
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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