VBscript Set LDAP object doesn't exist - how to handle the error

Hello,
I have a list of usernames in a CSV file that I need to retrieve LDAP properties for.  I'm using the following code.  If the object doesn't exist, how can I handle the error to truncate the loop and move onto the next user in the CSV file?


Set objDataFile = objFSO.OpenTextFile(strInputFile,ForReading)
Do until objDataFile.AtEndofStream
	arrStr = Split(objDataFile.ReadLine,",")
	strSAMAcctName = arrStr(2)
 
	Set User = GetObject ("LDAP://CN=" & strSAMAcctName & StaffOU)
     
	        OutFile1.WriteLine User.sAMAccountName & "," & User.carLicense & User.displayName & "," & User.FirstName & "," & User.LastName
 
loop

Open in new window

jloomansAsked:
Who is Participating?
 
sirbountyCommented:

Set objDataFile = objFSO.OpenTextFile(strInputFile,ForReading)
 
Do until objDataFile.AtEndofStream
        arrStr = Split(objDataFile.ReadLine,",")
        strSAMAcctName = arrStr(2)
        On Error Resume Next 'Added this line to skip error occurrence 
        Set User = GetObject ("LDAP://CN=" & strSAMAcctName & StaffOU)
        If Err.Number = 0 Then
          OutFile1.WriteLine User.sAMAccountName & "," & User.carLicense & User.displayName & "," & User.FirstName & "," & User.LastName
        Else 
          'Need to report on errors?
          'Use wscript.echo Err.Description if you want to see the error on screen.
        End If
       On Error Goto 0 'Reset error count
loop

Open in new window

0
 
David LeeCommented:
Hi, jloomans.

Use On Error Resume Next and test the Err.Number. Something like the code below.  The Err.Number is usually 0 if no error occured.
Set objDataFile = objFSO.OpenTextFile(strInputFile,ForReading)
On Error Resume Next
Do until objDataFile.AtEndofStream
    arrStr = Split(objDataFile.ReadLine,",")
    strSAMAcctName = arrStr(2)
    Set User = GetObject ("LDAP://CN=" & strSAMAcctName & StaffOU)
    If Err.Number <> 0 Then
        OutFile1.WriteLine User.sAMAccountName & "," & User.carLicense & User.displayName & "," & User.FirstName & "," & User.LastName
    End If
Loop

Open in new window

0
 
David LeeCommented:
LOL.  SB type faster than I do.
0
 
sirbountyCommented:
:^)
0
 
jloomansAuthor Commented:
Thanks! This should work.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.