Consulting

Results 1 to 3 of 3

Thread: determining user domain account (AD) from VBA

  1. #1
    VBAX Newbie
    Joined
    May 2008
    Posts
    2
    Location

    determining user domain account (AD) from VBA

    Hello All,
    I am writing a simple VBA application (the code lives in .xls file). To open my .xls file, I run Excel using "Run As..." option. Is there any way to programmatically determine from VBA who the user is (domain\account) that is currently opening this file?

    This is done in Windows server 2003 environment, with Active Directory.

    If there is a solution to this problem, will the same work for XP or Vista?

  2. #2
    VBAX Newbie
    Joined
    May 2008
    Posts
    2
    Location
    There is environ"USERNAME" function, but that seems to only return user name, without the domain name...
    Anybody knows how to get domain name, without calling win api, or .NET classes?

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Here is a routine I half-inched from Randy Birch some years ago

    [vba]

    Option Explicit

    Private Declare Function LookupAccountName Lib "advapi32" Alias "LookupAccountNameA" ( _
    ByVal lpSystemName As String, _
    ByVal lpAccountName As String, _
    Sid As Byte, _
    cbSid As Long, _
    ByVal DomainName As String, _
    cbDomainName As Long, _
    peUse As Long) As Long


    Public Sub TestIt()
    Dim Account As String 'account name of interest
    Dim System As String 'specifying the system
    Dim Domain As String 'domain validating user
    Dim Details As String

    Account = Environ("Username")
    System = ""
    Domain = ""

    Select Case ValidateUser(Account, Domain, System)
    Case True: Details = Account & " - " & Domain
    Case False: Details = "User not found."
    End Select

    MsgBox Details

    End Sub


    Public Function ValidateUser(Optional ByRef AccountName As String, _
    Optional ByRef DomainName As String, _
    Optional ByVal SystemName As String) As Boolean
    Dim success As Long
    Dim cbSid As Long
    Dim cbDomainName As Long
    Dim peUse As Long
    Dim bSID() As Byte

    If AccountName = "" Then AccountName = Environ("Username")
    DomainName = vbNullString
    cbDomainName = 0

    If Len(SystemName) = 0 Then SystemName = vbNullString
    success = LookupAccountName(SystemName, AccountName, 0&, cbSid, DomainName, cbDomainName, peUse)
    If (success = 0) And (cbSid > 0) Then

    DomainName = Space$(cbDomainName)
    ReDim bSID(0 To cbSid - 1)
    success = LookupAccountName(SystemName, AccountName, bSID(0), cbSid, DomainName, cbDomainName, peUse)
    If success > 0 Then

    If cbDomainName > 0 Then DomainName = Left$(DomainName, cbDomainName)
    End If
    End If

    ValidateUser = success

    End Function
    [/vba]
    Last edited by Bob Phillips; 05-21-2008 at 02:50 PM.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •