Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

use windows authentication in VBA for excel

Status
Not open for further replies.

Mago1234

Programmer
May 13, 2003
5
AU
Hi,

I need to build an Excel application (excel 2000) that uses Windows authentication. That is to say, when the user clicks on the excel spread sheet, he is required to enter his Windows user name and password before he can view the content of the spreadsheet. I was told that in the program I need to call some Windows API's, but I don't know how to do it, and there seems to be nowhere to be found on the Web. I know how to read a user's Windows login name via calling mpr.dll. But how to enforce a windows authentication before the user can open the file?

Thanks.

 
Please suggest why the following code is not working.
===================
===================

Private Declare Function LogonUser _
Lib "Advapi32" Alias "LogonUserA" _
(ByVal lpszUsername As String, _
ByVal lpszDomain As Any, _
ByVal lpszPassword As String, _
ByVal dwLogonType As Long, _
ByVal dwLogonProvider As Long, _
phToken As Long) As Long

' Constants used by LogonUser
Private Const LOGON32_PROVIDER_DEFAULT As Long = 0&
Private Const LOGON32_PROVIDER_WINNT35 As Long = 1&
Private Const LOGON32_LOGON_INTERACTIVE As Long = 2&
Private Const LOGON32_LOGON_NETWORK As Long = 3&
Private Const LOGON32_LOGON_BATCH As Long = 4&
Private Const LOGON32_LOGON_SERVICE As Long = 5&


Private Function Login(ByVal xi_strUserID As String, _
ByVal xi_strPassword As String) As Boolean

On Error Resume Next ' Don't accept errors here

Dim p_lngToken As Long
Dim p_lngRtn As Long

p_lngRtn = LogonUser(xi_strUserID, _
0&, _
xi_strPassword, _
LOGON32_LOGON_INTERACTIVE, _
LOGON32_PROVIDER_DEFAULT, _
p_lngToken)

If p_lngRtn = 0 Then
Login = False
Else
Login = True
End If

On Error GoTo 0
End Function

===
In a user form, it's coded to call the function Login

Private Sub cmdOK_Click()

Dim strPassword As String
Dim strUserID As String

strUserID = "user1"

Me.Tag = txtPassword.Text
Me.Hide

strPassword = txtPassword.Text

If Login(strUserID, strPassword) Then
MsgBox ("your password is correct")
Else
MsgBox ("your password is incorrect")
ActiveWorkbook.close
End If

=======

I am wondering why everytime when the password is correctly entered for user "user1", it always returns the message "your password is incorrect". What goes wrong in the above code?

Thanks in advance for any suggestions and help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top