Solved

Add Error Handling to code

Posted on 2013-05-13
2
386 Views
Last Modified: 2013-05-15
I have the following code.  It runs fine unless one of the PCs in the Array is not available.  Then it stops and all of the PCs after the failed one log no data.  

How can I manage the error so it continues throught the whole array?


Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForAppending = 2
Dim objFSO:Set objFSO = CreateObject("Scripting.FileSystemObject")

Dim myDateString0, myDateString1, myDateString2, break
myDateString = FormatDateTime(Date(), 0)
fileDate = Replace(myDateString, "/", "-") & "-" & Replace(Time, ":", "-")

Dim RunStamp
RunStamp = FormatDateTime(Now(),0)

'set up log file - edit this to change path and name
'LogFile = "\\eis62pc\SummitHealthcare\DRS Desktop 3\Reports\" & "audit-" & fileDate & ".txt"
LogFile = "C:\Summit\" & "audit-" & fileDate & ".txt"
Dim objLogFile:Set objLogFile = objFSO.CreateTextFile(logfile, 2, True)

'create an array of workstations - add more to the end of this list if you want
Set workstationList = CreateObject( "System.Collections.ArrayList" )
workstationList.Add "2MNA24PC"
workstationList.Add "3MSB11PC"
workstationList.Add "4MSB23PC"
workstationList.Add "5MNA11PC"
workstationList.Add "E2E05PC"
workstationList.Add "E3W08PC"
workstationList.Add "ECPOE15PC"
workstationList.Add "EIS42PC"
workstationList.Add "EIS62PC"
workstationList.Add "EIS58PC"
workstationList.Add "ENA05PC"
workstationList.Add "EIS38PC"

'workstationList.Add "D"
'workstationList.Add "C"


'Print banner
'objLogFile.Write "Offline Report Audit"
'objLogFile.Writeline "Listing for: " & objFolder.Path
'objLogFile.Writeline "Run Date: " & myDateString & " " & Time
'objLogFile.Writeline

'loop through all of the files
For Each strItem in workstationList
      'path to files to list
      objStartFolder = "\\" & strItem & "\SummitHealthcare\DRS Desktop 3\Reports"
      Set objFolder = objFSO.GetFolder(objStartFolder)
      Set colFiles = objFolder.Files
      ShowSubfolders objFSO.GetFolder(objStartFolder)
Next


Sub ShowSubFolders(Folder)
        For Each objFile in colFiles
            'objLogFile.Write chr(34) & strItem & chr(34) & "," & chr(34) & objFile.Name & chr(34) & "," & chr(34) & objFile.DateLastModified & chr(34)
                  objLogFile.Write chr(34) & strItem & chr(34) & "," & chr(34) & objFile.Name & chr(34) & "," & chr(34) & objFile.DateLastModified & chr(34) & "," & chr(34) & RunStamp & chr(34) 'FormatDateTime(Now(),0)  
            objLogFile.Writeline
        Next
   
End Sub

objLogFile.Close
0
Comment
Question by:Scotto111
2 Comments
 
LVL 45

Assisted Solution

by:Martin Liss
Martin Liss earned 225 total points
ID: 39162830
Try adding On Error Resume Next near the top.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 225 total points
ID: 39163306
Or you could change these lines
      Set objFolder = objFSO.GetFolder(objStartFolder)
      Set colFiles = objFolder.Files
      ShowSubfolders objFSO.GetFolder(objStartFolder)

Open in new window


to this
      If objFSO.FolderExists(objStartFolder) = True Then
	      Set objFolder = objFSO.GetFolder(objStartFolder)
	      Set colFiles = objFolder.Files
	      ShowSubfolders objFSO.GetFolder(objStartFolder)
      End If

Open in new window


so that you confirm whether the folder is accessible first.

Regards,

Rob.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now