Give an example of how to use 'On Error Resume Next' to handle a potential error when opening a file.

Responsive Ad Header

Question

Grade: Education Subject: Support
Give an example of how to use 'On Error Resume Next' to handle a potential error when opening a file.
Asked by:
101 Viewed 101 Answers
Responsive Ad After Question

Answer (101)

Best Answer
(452)
```vba Sub OpenFile() Dim fso As Object, file As Object On Error Resume Next Set fso = CreateObject("Scripting.FileSystemObject") Set file = fso.OpenTextFile("C:\InvalidPath\file.txt", 1) If Err.Number <> 0 Then MsgBox "File not found or invalid path!" Err.Clear End If On Error GoTo 0 If Not file Is Nothing Then 'Process the file Debug.Print file.ReadAll() Else MsgBox "Error reading file." End If End Sub ```