Querying Active Directory

ScotTFO

Board Regular
Joined
May 30, 2008
Messages
72
Is there a way to use VBA to query my active directory to translate a user login to the name?

Thanks!
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Not sure why the active directory is needed.

I use this to get a users name.
Code:
Sub t()
  MsgBox Environ("username")
End Sub
 
Upvote 0
Sorry I think I may not have explained the sitaution well enough.

I have a list of login names that are configured in our Active Directory server. I need to find a way to query the server with those login names and have it pull the full names out.

This script will not be run on all the users machines, only mine.
 
Upvote 0
Are these filenames in a folder that you need? If so, I would use something like this to get the filenames into an array.

Code:
Function FindFiles(sRootFolder As String, sFiles As String) As Variant
     
    Dim fs As Object
    Dim strFilename As String
    Dim i As Long, LastRow As Long
    Dim a() As Variant
     
    Set fs = Application.FileSearch
    With fs
        .LookIn = sRootFolder
        .Filename = sFiles 'set your filename or extension with wilcards if needed.
        .SearchSubFolders = True
        LastRow = .FoundFiles.Count
        If .Execute() > 0 Then
            For i = 1 To LastRow
                strFilename = .FoundFiles(i)
                ReDim Preserve a(i - 1)
                a(i - 1) = strFilename
            Next i
        Else
            MsgBox "No files found", vbCritical
        End If
    End With
    
    FindFiles = a()
     
End Function
 
Upvote 0
You should be able to tweak Grizlore's code such that it loops the call to GetUserName but parses a different username each time which would be based on your own list of usernames along the lines of:

Code:
Sub ....

Dim rng as Range
Set rng = Sheets("Sheet1").Range("A1:A10")

For Each cell in rng
Set objInfo = CreateObject("ADSystemInfo") 
strLDAP = cell.value
Set objInfo = Nothing 
strFullName = GetUserName(strLDAP)
cell.offset(0,1) = strFullName 'put name in cell next to username
Next cell

End Sub

With Grizlore's post containing the actual GetUserName function

Obviously this isn't my post but presumably this would work...
 
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,825
Members
449,096
Latest member
Erald

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