Microsoft . 技术之路...

—— 专注于微软技术, 分享是快乐的源泉......
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

List Active Directory Data in a Spreadsheet

Posted on 2006-07-11 08:43  赣江源  阅读(233)  评论(0编辑  收藏  举报

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")