Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Vbscript unable to trap error into excel
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Sajit Nair  
View profile  
 More options Nov 7 2009, 6:27 pm
Newsgroups: microsoft.public.scripting.vbscript
From: Sajit Nair <sajit.nai...@gmail.com>
Date: Sat, 7 Nov 2009 10:27:43 -0800 (PST)
Local: Sat, Nov 7 2009 6:27 pm
Subject: Vbscript unable to trap error into excel
Hi Guys,

I am trying to grab some information about a list of servers into
excel files, which works fine. What i am unable to do is trap the
error and fill the error into the excel cells. If the bind to the
remote machine does not work it still gives the headers instead of the
bind failure message.

--------------------------------------------------------------------------- -----------------
If (strComputer <> "") Then
                Set colAccounts = GetObject("WinNT://" & strComputer & "")
                Set colGroups = GetObject("WinNT://" & strComputer & "")
                colAccounts.Filter = Array("user")
                colGroups.Filter = Array("group")
      On Error Resume Next
      Err.Clear
      If Err.Number <> 0 Then
                objExcel.Cells(1, 1).Value = "Failed to bind to " & strComputer
        Else
                objExcel.Cells(1, 1).Value = "User Accounts"
                objExcel.Cells(1, 2).Value = "Full Name"
                objExcel.Cells(1, 3).Value = "Last Logon TimeStamp"
-------------------------------------

Second part of the script where error trapping is not working, it just
shows a blank cell wherever the last logon information is not
available. As i already said the rest works just fine.

-------------------------------------
                intCol = 3
                On Error Resume Next
                Err.Clear
                If Err.Number = 0 Then
                objExcel.Cells(intRow, intCol).Value = objUser.LastLogin
                Else
                objExcel.Cells(intRow, intCol).Value = "Never"
                End If
---------------------------------

Any help would be highly appreciated. Thank you and Regards

Sajit Nair


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Richard Mueller [MVP]  
View profile  
 More options Nov 7 2009, 6:52 pm
Newsgroups: microsoft.public.scripting.vbscript
From: "Richard Mueller [MVP]" <rlmueller-nos...@ameritech.nospam.net>
Date: Sat, 7 Nov 2009 12:52:59 -0600
Local: Sat, Nov 7 2009 6:52 pm
Subject: Re: Vbscript unable to trap error into excel

"Sajit Nair" <sajit.nai...@gmail.com> wrote in message

news:9e6b3a3a-1df0-4efd-ad9f-6609bbae847c@t2g2000yqn.googlegroups.com...

The "On Error Resume Next" statement should be just before the statement
expected to possibly raise an error. For example:
=========
If (strComputer <> "") Then
    On Error Resume Next
    Set colAccounts = GetObject("WinNT://" & strComputer)
    Set colGroups = GetObject("WinNT://" & strComputer)
    If (Err.Number <> 0) Then
        On Error GoTo 0
        objExcel.Cells(1, 1).Value = "Failed to bind to " & strComputer
    Else

        On Error GoTo 0
        colAccounts.Filter = Array("user")
        colGroups.Filter = Array("group")
        objExcel.Cells(1, 1).Value = "User Accounts"
        objExcel.Cells(1, 2).Value = "Full Name"
        objExcel.Cells(1, 3).Value = "Last Logon TimeStamp"
========
The statement "On Error GoTo 0" restores normal error handling, so
unexpected errors don't get ignored. The statements "On Error Resume Next"
and "On Error GoTo 0" both clear any error condition that exists at the
point, so there is no need to use "Err.Clear".

I can't tell what you are doing later, where you use objUser.LastLogin.
Possibly it should be similar to:
=======
For Each objUser In colAcccounts
    On Error Resume Next
    objExcel.Cells(intRow, intCol).Value = objUser.LastLogin
    If (Err.Number <> 0) Then
        On Error GoTo 0
        objExcel.Cells(intRow, intCol).Value = "Never"
    End If
    On Error GoTo 0
Next

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sajit Nair  
View profile  
 More options Nov 8 2009, 3:38 am
Newsgroups: microsoft.public.scripting.vbscript
From: Sajit Nair <sajit.nai...@gmail.com>
Date: Sat, 7 Nov 2009 19:38:03 -0800 (PST)
Local: Sun, Nov 8 2009 3:38 am
Subject: Re: Vbscript unable to trap error into excel
Richard,

Thanks for your response. I made the modifications as suggested. The
first error trap fails but the second one does succeed. Below are both
parts in complete

-----------------------------

     If (strComputer <> "") Then
           On Error Resume Next
           Set colAccounts = GetObject("WinNT://" & strComputer)
           Set colGroups = GetObject("WinNT://" & strComputer)
           If (Err.Number <> 0) Then
        On Error GoTo 0
        objExcel.Cells(1, 1).Value = "Failed to bind to " & strComputer
        Else
          On Error GoTo 0
          colAccounts.Filter = Array("user")
          colGroups.Filter = Array("group")
          objExcel.Cells(1, 1).Value = "User Accounts"
          objExcel.Cells(1, 2).Value = "Full Name"
          objExcel.Cells(1, 3).Value = "Last Logon TimeStamp"
             For Each objUser In colAccounts
                intRow = intRow + 1
                intCol = 1
                objExcel.Cells(intRow, intCol).Value = objUser.Name
                intCol = 2
                objExcel.Cells(intRow, intCol).Value = objUser.FullName
                intCol = 3
                On Error Resume Next
                objExcel.Cells(intRow, intCol).Value = objUser.LastLogin
                If (Err.Number <> 0) Then
                On Error GoTo 0
                objExcel.Cells(intRow, intCol).Value = "Never"
                End If
                On Error GoTo 0
             Next
                intRow = intRow + 3
                objExcel.Cells(intRow, 1).Value = "Local Security Groups"
                objExcel.Cells(intRow, 2).Value = "Group Members"
         For Each objGroup In colGroups
                intRow = intRow + 1
                intCol = 1
                objExcel.Cells(intRow, intCol).Value = objGroup.Name
                     For Each objUser in objGroup.Members
                                intCol = intCol + 1
                objExcel.Cells(intRow, intCol).Value = objUser.Name
             Next
         Next
         End If
    End If
 ----------------------

Regards

Sajit


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Richard Mueller [MVP]  
View profile  
 More options Nov 9 2009, 5:26 pm
Newsgroups: microsoft.public.scripting.vbscript
From: "Richard Mueller [MVP]" <rlmueller-nos...@ameritech.nospam.net>
Date: Mon, 9 Nov 2009 11:26:51 -0600
Local: Mon, Nov 9 2009 5:26 pm
Subject: Re: Vbscript unable to trap error into excel

"Sajit Nair" <sajit.nai...@gmail.com> wrote in message

news:00045311-2d6d-4477-be85-fca787785a56@y10g2000prg.googlegroups.com...

I would replace this snippet of your code:
===========
        Set colAccounts = GetObject("WinNT://" & strComputer & "")
        If Err <> 0 Then
            objExcel.Cells(1, 1).Value = "Failed to bind to " & strComputer
        Else
            objExcel.Cells(1, 1).Value = "Users"
=============
With this:
============
        On Error Resume Next
        Set colAccounts = GetObject("WinNT://" & strComputer)
        If (Err.Number) <> 0 Then
            On Error GoTo 0
            objExcel.Cells(1, 1).Value = "Failed to bind to " & strComputer
        Else
            On Error GoTo 0
            objExcel.Cells(1, 1).Value = "Users"
========
Also, I would replace this snippet from later in your code:
===========
                On Error Resume Next
                If Err = 0 Then
                    objExcel.Cells(intRow, intCol).Value = objUser.LastLogin
                Else
                    objExcel.Cells(intRow, intCol).Value = "Never"
                End If
===========
With this:
========
                On Error Resume Next
                If (Err.Number = 0) Then
                    On Error GoTo 0
                    objExcel.Cells(intRow, intCol).Value = objUser.LastLogin
                Else
                    On Error GoTo 0
                    objExcel.Cells(intRow, intCol).Value = "Never"
                End If
===========
If you do not restore normal error handling, subsequent errors will be
ignored, which besides making troubleshooting nearly impossible, could also
yield incorrect results.

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2010 Google