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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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

David LeeCommented:
LOL.  SB type faster than I do.
sirbountyCommented:
:^)
jloomansAuthor Commented:
Thanks! This should work.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.