Reference page: Active Directory – Sample Scripts (Excel/VBA)
How to retrieve Security Group
Build LDAP Query – Execute Query – Catch Errors
Note. You need to login as an authorized person to successfully execute the script.
' 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" ' Security Groups ' Group01 - belongs to Company01
How to retrieve Active Directory Security Group (Excel/VBA source code)
Note that Security Group is an object that is stored within Organizational Unit therefore LDAP query should specify the exact location.
'Build LDAP query to get an object sQuery = "LDAP:// cn=Group01, 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 Security Group | Query = " & sQuery End If