Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get a list of all usernames in a domain, and put them in a text file 1

Status
Not open for further replies.

mark01

Technical User
Jan 17, 2001
600
US
How would I get a list of all usernames for a particular domain, and write them into a text file. For example, I'd like to get a list of all users in DOMAIN1, and place them in a text file called text1.txt.

Then I'd like the text file to look like this...
jwilson
abrooks
jsmith
etc..

Thanks
 
Here's a start:

Private Sub ListUsers()
Dim objComputer
Dim objUser
Set objComputer = GetObject("WinNT://" & PRIMARY_USER_ACCOUNT_DOMAIN)
objComputer.Filter = Array("User")
For Each objUser In objComputer
Debug.Print objUser.Name
Next
End Sub
 
Ok, that worked, thank you! Now how would I enter that into a text file, erasing the whole text file before I enter the usernames?

Thanks for the help
 
Try this:

Private Sub ListUsers()
Dim objComputer
Dim objUser
Set objComputer = GetObject("WinNT://" & PRIMARY_USER_ACCOUNT_DOMAIN)
objComputer.Filter = Array("User")
Dim FileNum As Integer
FileNum = FreeFile
Open "C:\YourFileName.txt" For Output As #intFileNum
For Each objUser In objComputer
Print #intFileNum, objUser.Name
Next
Close intFileNum
End Sub
 
where javajoe has debug.print objuser.name just open a text file and write to it.

open "c:\mytextfile.txt" for output as #1
print #1, objuser.name & vbcrlf
close #1

you can put the open and close outside of the for each loop if you want, one way it will just open and close it for every name to write, outside of the for each loop you will open it, write all the names then close it.
 
OK, i tried both of those, and i get an error in two places. On DrJavaJoe, i get it on Dim FileNum As Integer, and on sciophyte, i get it on Open "c:\mytextfile.txt" for output as #1.
This is the error...
"expected end of statement"
 
Dim FileNum As Integer
FileNum = FreeFile

should have been:

Dim intFileNum As Integer
intFileNum = FreeFile

but I don't see why you would be getting that error.
 
OK, i am just retarded. I works! Thanks for all of your help!
 
Ok, one more question i swear. :) Instead of the text file, how would i add it to a combo box?
 
Replace
Print #intFileNum, objUser.Name
With
Combo1.additem objUser.Name
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top