To get the line number where the error occurred, you need to use the
Erl
function. However, this will only work if your code has line numbers; otherwise, the function will always return
0
.
On Error Resume Next
10: Dim a, b, c
20: a = 10
30: b = 0
40: c = a / b
If Err.Number <> 0 Then
Dim fso, file, filePath
Set fso = CreateObject("Scripting.FileSystemObject")
filePath = fso.GetParentFolderName(WScript.ScriptFullName) & "\error.txt"
Set file = fso.CreateTextFile(filePath, True)
file.WriteLine "Error on line " & Erl & ": " & Err.Number & " - " & Err.Description
file.Close
Set file = Nothing
Set fso = Nothing
End If
On Error GoTo 0