Description

Demonstration script that retrieves data from Active Directory and then displays that data in an Excel spreadsheet.

Script Code

Const ADS_SCOPE_SUBTREE = 2

Set objExcel = CreateObject("Excel.Application")

objExcel.Visible 
= True
objExcel.Workbooks.Add

objExcel.Cells(
11).Value = "Last name"
objExcel.Cells(
12).Value = "First name"
objExcel.Cells(
13).Value = "Department"
objExcel.Cells(
14).Value = "Phone number"

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 givenName, SN, department, telephoneNumber FROM " _
        
& "'LDAP://dc=fabrikam,dc=microsoft,dc=com' WHERE " _
            
& "objectCategory='user'"  
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
= 2

Do Until objRecordSet.EOF
    objExcel.Cells(x, 
1).Value = _
        objRecordSet.Fields(
"SN").Value
    objExcel.Cells(x, 
2).Value = _
        objRecordSet.Fields(
"givenName").Value
    objExcel.Cells(x, 
3).Value = _
        objRecordSet.Fields(
"department").Value
    objExcel.Cells(x, 
4).Value = _
        objRecordSet.Fields(
"telephoneNumber").Value
    x 
= x + 1
    objRecordSet.MoveNext
Loop

Set objRange = objExcel.Range("A1")
objRange.Activate

Set objRange = objExcel.ActiveCell.EntireColumn
objRange.Autofit()

Set objRange = objExcel.Range("B1")
objRange.Activate
Set objRange = objExcel.ActiveCell.EntireColumn
objRange.Autofit()

Set objRange = objExcel.Range("C1")
objRange.Activate

Set objRange = objExcel.ActiveCell.EntireColumn
objRange.Autofit()

Set objRange = objExcel.Range("D1")
objRange.Activate

Set objRange = objExcel.ActiveCell.EntireColumn
objRange.Autofit()

Set objRange = objExcel.Range("A1").SpecialCells(11)
Set objRange2 = objExcel.Range("C1")
Set objRange3 = objExcel.Range("A1")

相关文章:

  • 2021-11-23
  • 2021-09-20
  • 2022-12-23
  • 2021-07-09
  • 2021-06-07
  • 2022-01-01
  • 2021-09-26
猜你喜欢
  • 2022-12-23
  • 2021-09-19
  • 2021-12-23
  • 2021-06-24
  • 2021-09-12
  • 2022-03-09
相关资源
相似解决方案