Solved

Add Error Handling to code

Posted on 2013-05-13
2
395 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 48

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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month6 days, 5 hours left to enroll

627 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