I am experiencing an issue with my VBScript.
I have below code working fine when I run it directly (by double clicking on it, it's triggering wscript
). I have some 1000 .xls files in FilePath
.
FilePath = "c:\test"
Set SourceFolder = objFSO.GetFolder(FilePath+"\")
For Each file In SourceFolder.Files
If Right(LCase(file.Name), 4)=".xls" Then
OutFile = OutFilePath + "\" + Left(file.Name, Len(file.Name)-4) + ".csv"
Set ExcelObject = oExcel.Workbooks.Open(file.Path)
If Err.Number <> 0 Then objLogFile.WriteLine "Exception occured(1): " + Err.Decscription
RowCount = oExcel.ActiveWorkbook.Sheets(1).UsedRange.Rows.Count
ColumnCount = oExcel.ActiveWorkbook.Sheets(1).UsedRange.Columns.Count
For i=1 To RowCount
For j=1 To ColumnCount
inText = ExcelObject.Sheets(1).Cells(i,j).Value
inText = Replace(inText, vbCr, " ")
inText = Replace(inText, vbLf, " ")
inText = Replace(inText, ",", " ")
ExcelObject.Sheets(1).Cells(i,j).Value = inText
Next
Next
ExcelObject.SaveAs OutFile, 6
ExcelObject.Close False
End If
Next
If I schedule the same script in the Windows Task Scheduler, it is not throwing any error also, its just running (I can see wscript
running in the Task Manager).
I tried to capture what error it is throwing, but I couldn't capture. After writing some log next to every line, I got to know that it is giving error in the below line.
Set ExcelObject = oExcel.Workbooks.Open(file.path)
I tried to capture error by putting the statements On Error Resume Next
and below If
condition (handled all the ObjLogFile
object related code)
If Err.Number <> 0 Then objLogFile.WriteLine "Exception occured(1): " + Err.Decscription
Still it is not capturing the error.
Please help me, how to proceed with the error with the following line. Though it is working fine when I run directly, it's not working when I schedule.
Set ExcelObject = oExcel.Workbooks.Open(file.path)