[英]Opening excel file working fine, but if I schedule the script through Windows Task Scheduler, it's not working
I am experiencing an issue with my VBScript.我的 VBScript 出现问题。
I have below code working fine when I run it directly (by double clicking on it, it's triggering wscript
).当我直接运行它时,我有以下代码工作正常(通过双击它,它正在触发
wscript
)。 I have some 1000 .xls files in FilePath
.我在
FilePath
有大约 1000 个 .xls 文件。
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).如果我在 Windows 任务计划程序中安排相同的脚本,它也不会抛出任何错误,它只是在运行(我可以看到
wscript
在任务管理器中运行)。
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)我试图通过将语句放在
On Error Resume Next
和If
条件之ObjLogFile
捕获错误(处理了所有ObjLogFile
对象相关的代码)
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)
Since I am using Excel Objects, which are interactive, I cann't give the option in the scheduler "Run whether user is logged on or not"由于我使用的是交互式 Excel 对象,因此我无法在调度程序中提供选项“无论用户是否登录都运行”
I changed this option to "Run only when user is logged on"我将此选项更改为“仅在用户登录时运行”
Its working now.它现在工作。 Thank you all for your suggestions.
谢谢大家的建议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.