title: VBscript error handling - move to a next item in a "For Each" loop
body: <p>I have a loop that processes each file of a folder. In case a file is corrupted and can't be opened, I want to:
  - Display the error message to the user
  - Stop the execution of the remaining code
  - Loop to the next file</p>
  
  <p>The problem is, that if the file cannot be opened at the beginning of the loop, there's still a lot of code that will execute, which will throw other errors. I cannot exit the loop, since this would mean that not all of the files would get processed, and I cannot continue the execution of the code. Please see the part of the code below.</p>
  
  <pre><code>Set fso = CreateObject("Scripting.FileSystemObject")
  For each objFile in fso.GetFolder(".").Files
      If lCase(fso.GetExtensionName(objFile)) = srcExtName Then
          Set app = GetNewAppInstance (Program)
          Set fileHandle = GetNewFileHandler(app)
  
          On Error Resume Next
              Set objOpenFile = fileHandle.Open(objFile.path)
              If err.number &lt;&gt; 0 Then 
              next
                  MsgBox "The file " &amp; objFile.name &amp; " cannot be opened. Please verify that the file is not corrupted or locked." &amp; vbNewLine &amp; "The file will be skipped."
                  Set fileHandle = nothing : app.Quit : Set app = nothing
                  &lt;&lt;&lt; How can I jump to the next file from here ? &gt;&gt;&gt;
              End If
          On error goto 0
          strSrcPath = fso.BuildPath(objOpenFile.path, fso.GetBaseName(objOpenFile.name) &amp; "._temp")
          objOpenFile.SaveAs strSrcPath, tmpExtNum
          objOpenFile.Close
  
          Set objOpenFile = nothing 
          Set fileHandle = nothing 
          app.Quit : Set app = nothing
  
          Set app = GetNewAppInstance (Program)
          Set fileHandle = GetNewFileHandler(app)
  
          Set objOpenFile = fileHandle.Open(strSrcPath)
          strTgtPath = fso.BuildPath(objOpenFile.path, fso.GetBaseName(objOpenFile.name) &amp; "." &amp; srcExtName)
          objOpenFile.SaveAs strTgtPath, srcExtNum
          objOpenFile.Close
          fso.DeleteFile(strSrcPath)
  
          Set objOpenFile = nothing 
          Set fileHandle = nothing 
          app.Quit : Set app = nothing
      End If
  Next
  End Sub
  </code></pre>
  
  <p>Thanks!</p>