Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Add Error Handling to code

Posted on 2013-05-13
2
Medium Priority
?
397 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 49

Assisted Solution

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

Accepted Solution

by:
RobSampson earned 900 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

721 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