Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Vbscript unable to trap error into excel

Reply
 
Old 3 Weeks Ago   #1 (permalink)
Sajit Nair


 
 

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

My System SpecsSystem Spec
Old 3 Weeks Ago   #2 (permalink)
Richard Mueller [MVP]


 
 

Re: Vbscript unable to trap error into excel


"Sajit Nair" <sajit.nair.7@newsgroup> wrote in message
news:9e6b3a3a-1df0-4efd-ad9f-6609bbae847c@newsgroup
Quote:

> 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
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
--


My System SpecsSystem Spec
Old 3 Weeks Ago   #3 (permalink)
Sajit Nair


 
 

Re: Vbscript unable to trap error into excel

Richard,

Thanks for the response. I made the modifications as suggested by you.
The first trap is still not working but the second is.
Here is the complete script.
---------------------------------------------------------

Set objShell = CreateObject("WScript.Shell")
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objExcel = CreateObject("Excel.Application")
Set FileSystem = WScript.CreateObject("Scripting.FileSystemObject")
objExcel.Visible = True
inputFile = "c:\Servers.txt"
set objTS = objFS.OpenTextFile(inputFile,1)
Workbook = 1
counter = 1
Set objWorksheet = objExcel.Workbooks.Add
Do until objTS.AtEndOfStream
strComputer = Trim(objTS.readline)
If counter > 3 Then
set objWorksheet = objExcel.Sheets.Add( , objExcel.WorkSheets
(objExcel.WorkSheets.Count))
End If
objExcel.worksheets(counter).Activate
objExcel.worksheets(counter).Name = strComputer
intRow = 1
intCol = 1
If (strComputer <> "") Then
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"
objExcel.Cells(1, 2).Value = "Last Logon"
objExcel.Cells(1, 3).Value = "Groups"
objExcel.Cells(1, 4).Value = "Group Members"
colAccounts.Filter = Array("user")
For Each objUser In colAccounts
intRow = intRow + 1
intCol = 1
objExcel.Cells(intRow, intCol).Value = objUser.Name
intCol = 2
On Error Resume Next
If Err = 0 Then
objExcel.Cells(intRow, intCol).Value = objUser.LastLogin
Else
objExcel.Cells(intRow, intCol).Value = "Never"
End If
Next
intRow = 1
Set colGroups = GetObject("WinNT://" & strComputer & "")
colGroups.Filter = Array("group")
For Each objGroup In colGroups
intRow = intRow + 1
intCol = 3
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
counter = counter +1
Loop
msgBox "Finished"
-----------------------------------------------------------------

Regards

Sajit
My System SpecsSystem Spec
Old 3 Weeks Ago   #4 (permalink)
Sajit Nair


 
 

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
My System SpecsSystem Spec
Old 2 Weeks Ago   #5 (permalink)
Richard Mueller [MVP]


 
 

Re: Vbscript unable to trap error into excel


"Sajit Nair" <sajit.nair.7@newsgroup> wrote in message
news:00045311-2d6d-4477-be85-fca787785a56@newsgroup
Quote:

> Richard,
>
> Thanks for the response. I made the modifications as suggested by you.
> The first trap is still not working but the second is.
> Here is the complete script.
> ---------------------------------------------------------
>
> Set objShell = CreateObject("WScript.Shell")
> Set objFS = CreateObject("Scripting.FileSystemObject")
> Set objExcel = CreateObject("Excel.Application")
> Set FileSystem = WScript.CreateObject("Scripting.FileSystemObject")
> objExcel.Visible = True
> inputFile = "c:\Servers.txt"
> set objTS = objFS.OpenTextFile(inputFile,1)
> Workbook = 1
> counter = 1
> Set objWorksheet = objExcel.Workbooks.Add
> Do until objTS.AtEndOfStream
> strComputer = Trim(objTS.readline)
> If counter > 3 Then
> set objWorksheet = objExcel.Sheets.Add( , objExcel.WorkSheets
> (objExcel.WorkSheets.Count))
> End If
> objExcel.worksheets(counter).Activate
> objExcel.worksheets(counter).Name = strComputer
> intRow = 1
> intCol = 1
> If (strComputer <> "") Then
> 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"
> objExcel.Cells(1, 2).Value = "Last Logon"
> objExcel.Cells(1, 3).Value = "Groups"
> objExcel.Cells(1, 4).Value = "Group Members"
> colAccounts.Filter = Array("user")
> For Each objUser In colAccounts
> intRow = intRow + 1
> intCol = 1
> objExcel.Cells(intRow, intCol).Value = objUser.Name
> intCol = 2
> On Error Resume Next
> If Err = 0 Then
> objExcel.Cells(intRow, intCol).Value = objUser.LastLogin
> Else
> objExcel.Cells(intRow, intCol).Value = "Never"
> End If
> Next
> intRow = 1
> Set colGroups = GetObject("WinNT://" & strComputer & "")
> colGroups.Filter = Array("group")
> For Each objGroup In colGroups
> intRow = intRow + 1
> intCol = 3
> 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
> counter = counter +1
> Loop
> msgBox "Finished"
> -----------------------------------------------------------------
>
> Regards
>
> Sajit
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
--


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Exporting MsSQL table to Excel using vbScript? VB Script
Vbscript to Search Excel VB Script
VBScript to search LDAP from Excel function? VB Script
VBscript applying borders in Excel VB Script
new to excel and vbscript - need help VB Script


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46