Option Explicit Const ADS_SCOPE_SUBTREE = 2 Sub LoadUserInfo() Dim x, objConnection, objCommand, objRecordSet, oUser, oSamAccountName, omail, oDescription, otelephoneNumber, oCompany, otitle, odepartment Dim sht As Worksheet ' get domain Dim oRoot Set oRoot = GetObject("LDAP://rootDSE") 'Set oRoot = Dim sDomain sDomain = oRoot.Get("defaultNamingContext") Dim strLDAP strLDAP = "LDAP://" & sDomain Set objConnection = CreateObject("ADODB.Connection") Set objCommand = CreateObject("ADODB.Command") objConnection.Provider = "ADsDSOObject" objConnection.Open "Active Directory Provider" Set objCommand.ActiveConnection = objConnection objCommand.Properties("Page Size") = 100 objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE objCommand.CommandText = "SELECT adsPath FROM '" & strLDAP & "' WHERE objectCategory='person'AND objectClass='user'" Set objRecordSet = objCommand.Execute x = 2 Set sht = ThisWorkbook.Worksheets("Sheet1") With sht ' Clear and set Header info .Cells.Clear .Cells(1, 1).Value = "CN" .Cells(1, 2).Value = "Last Name" .Cells(1, 3).Value = "First Name" .Cells(1, 4).Value = "Display Name" .Cells(1, 5).Value = "SamAccountNanme" .Cells(1, 6).Value = "mail" .Cells(1, 7).Value = "Description" .Cells(1, 8).Value = "telephoneNumber" .Cells(1, 9).Value = "Company" .Cells(1, 10).Value = "Title" .Cells(1, 11).Value = "department" Do Until objRecordSet.EOF Set oUser = GetObject(objRecordSet.Fields("aDSPath")) .Cells(x, 1).Value = Replace(oUser.Name, "CN=", "") .Cells(x, 2).Value = oUser.SN .Cells(x, 3).Value = oUser.givenName .Cells(x, 4).Value = oUser.displayName .Cells(x, 5).Value = oUser.SamAccountName .Cells(x, 6).Value = oUser.mail .Cells(x, 7).Value = oUser.Description .Cells(x, 8).Value = oUser.telephoneNumber .Cells(x, 9).Value = oUser.Company .Cells(x, 10).Value = oUser.Title .Cells(x, 11).Value = oUser.department x = x + 1 objRecordSet.MoveNext Loop End With End Sub Private Sub Workbook_Open() LoadUserInfo End Sub |