Active Directory Scripting – retrieve User Account
Reference page: Active Directory – Sample Scripts (Excel/VBA)
How to retrieve User Account
Description
Build LDAP Query – Execute Query – Catch Errors
Note. You need to login as an authorized person to successfully execute the script.
Common definitions
' Domain: DEV.ENV.COM Dim sDomain, dvDC sDomain = "DEV.ENV.COM" dvDC = Split(sDomain, ".") ' Organizational Units ' Company01 - Active Directory Path: "dev.env.com/Main/Client/Company01" ' User Accounts ' User01 - belongs to Company01
How to retrieve Active Directory User Account (Excel/VBA source code)
Note that we enlist directory tree nodes backwars when building LDAP Path, and specify an exact path to the User Account.
'Build LDAP query to get an object
sQuery = "LDAP:// cn=User01, ou=Company01, ou=Client, ou=Main"
sQuery = sQuery & ",dc=" & dvDC(0) & ",dc=" & dvDC(1) & ", dc=" & dvDC(2)
'Execute query
On Error Resume Next
Set objUserAccount = GetObject(sQuery)
boolRC = (Err.Number <> 0)
On Error GoTo 0
'Error-handling
If boolRC Then
sReturnMessage = "Failed to retrieve User Account | Query = " & sQuery
End If

