Hi All,
I am try to adapt the VBS code below to search for users in an active directory and append an exsisting access 2007 table.
The code as is works but the output is in excel, but I would like to use it with access.
Can someone help with adapting the vbs code into access VBA module and get it to display the results on the screen for selection before appending the required user data into an exsisting table.
Regards,
Pank
Below is the VBS code.
' Bind to RootDSE - this object is used to
' get the default configuration naming context
' e.g. dc=microsoft,dc=co,dc=uk
set objRootDSE = getobject("ldap://RootDSE")
' Search box to search for user name
Name = InputBox("Enter Surname Name to search?")
' File name to export to
strExportFile = "C:user.xlsx"
' Root of search set to default naming context.
' e.g. dc=microsoft,dc=co,dc=uk
' RootDSE saves hard-coding the domain.
' If want to search within an OU rather than the domain,
' specify the distinguished name of the ou. e.g.
' ou=students,dc=microsoft,dc=co,dc=uk"
strRoot = objRootDSE.Get("DefaultNamingContext")
' Filter for user accounts - could be modified to search for specific users,
' such as those with mailboxes, users in a certain department etc.
strfilter = "(&(objectCategory=Person)(objectClass=User)(sn="&Name&"))"
' Attributes to return from the query
strAttributes = "sAMAccountName,givenName,sn," & _
"physicalDeliveryOfficeName," & _
"mail," & _
"title,department," & _
"company," & _
"l"
'Scope of the search. Change to "onelevel" if you didn't want to search child OU's
strScope = "subtree"
set cn = createobject("ADODB.Connection")
set cmd = createobject("ADODB.Command")
cn.open "Provider=ADsDSOObject;"
cmd.ActiveConnection = cn
cmd.commandtext = "<LDAP://" & strRoot & ">;" & strFilter & ";" & _
strAttributes & ";" & strScope
set rs = cmd.execute
' Use Excel COM automation to open Excel and create an excel workbook
set objExcel = CreateObject("Excel.Application")
set objWB = objExcel.Workbooks.Add
set objSheet = objWB.Worksheets(1)
' Copy Field names to header row of worksheet
For i = 0 To rs.Fields.Count - 1
objSheet.Cells(1, i + 1).Value = rs.Fields(i).Name
objSheet.Cells(1, i + 1).Font.Bold = True
Next
' Copy data to the spreadsheet
objSheet.Range("A2").CopyFromRecordset(rs)
' Save the workbook
objWB.SaveAs(strExportFile)
' Clean up
rs.close
cn.close
set objSheet = Nothing
set objWB = Nothing
objExcel.Quit()
set objExcel = Nothing
I am try to adapt the VBS code below to search for users in an active directory and append an exsisting access 2007 table.
The code as is works but the output is in excel, but I would like to use it with access.
Can someone help with adapting the vbs code into access VBA module and get it to display the results on the screen for selection before appending the required user data into an exsisting table.
Regards,
Pank
Below is the VBS code.
' Bind to RootDSE - this object is used to
' get the default configuration naming context
' e.g. dc=microsoft,dc=co,dc=uk
set objRootDSE = getobject("ldap://RootDSE")
' Search box to search for user name
Name = InputBox("Enter Surname Name to search?")
' File name to export to
strExportFile = "C:user.xlsx"
' Root of search set to default naming context.
' e.g. dc=microsoft,dc=co,dc=uk
' RootDSE saves hard-coding the domain.
' If want to search within an OU rather than the domain,
' specify the distinguished name of the ou. e.g.
' ou=students,dc=microsoft,dc=co,dc=uk"
strRoot = objRootDSE.Get("DefaultNamingContext")
' Filter for user accounts - could be modified to search for specific users,
' such as those with mailboxes, users in a certain department etc.
strfilter = "(&(objectCategory=Person)(objectClass=User)(sn="&Name&"))"
' Attributes to return from the query
strAttributes = "sAMAccountName,givenName,sn," & _
"physicalDeliveryOfficeName," & _
"mail," & _
"title,department," & _
"company," & _
"l"
'Scope of the search. Change to "onelevel" if you didn't want to search child OU's
strScope = "subtree"
set cn = createobject("ADODB.Connection")
set cmd = createobject("ADODB.Command")
cn.open "Provider=ADsDSOObject;"
cmd.ActiveConnection = cn
cmd.commandtext = "<LDAP://" & strRoot & ">;" & strFilter & ";" & _
strAttributes & ";" & strScope
set rs = cmd.execute
' Use Excel COM automation to open Excel and create an excel workbook
set objExcel = CreateObject("Excel.Application")
set objWB = objExcel.Workbooks.Add
set objSheet = objWB.Worksheets(1)
' Copy Field names to header row of worksheet
For i = 0 To rs.Fields.Count - 1
objSheet.Cells(1, i + 1).Value = rs.Fields(i).Name
objSheet.Cells(1, i + 1).Font.Bold = True
Next
' Copy data to the spreadsheet
objSheet.Range("A2").CopyFromRecordset(rs)
' Save the workbook
objWB.SaveAs(strExportFile)
' Clean up
rs.close
cn.close
set objSheet = Nothing
set objWB = Nothing
objExcel.Quit()
set objExcel = Nothing