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
 

steve1262

Board Regular
Joined
Aug 19, 2006
Messages
126
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???
 

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
7,561
Office Version
2016
Platform
Windows
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:

Forum statistics

Threads
1,085,864
Messages
5,386,404
Members
401,997
Latest member
cizwiz

Some videos you may like

This Week's Hot Topics

Top