![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | 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 =========== 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 Specs![]() |
![]() |
| 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 | |||