简体   繁体   English

打开 excel 文件工作正常,但如果我通过 Windows 任务计划程序安排脚本,则无法正常工作

[英]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 NextIf条件之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.

相关问题 在创建 excel 文件的任务计划程序窗口上运行脚本 - Run script on Task Scheduler windows where an excel file is created 通过任务计划程序运行时,Powershell脚本停止工作 - Powershell script stops working when ran through task scheduler 如何使用 Windows 任务计划程序为 xlsm 文件设置重复计划 - How to set recurring schedule for xlsm file using Windows Task Scheduler Excel 宏通过 powershell 运行,但在 Windows 任务计划程序运行时不运行 - Excel macros run through powershell but not when run by windows task scheduler Excel任务计划程序。 我收到错误消息“ Microsoft Excel已停止工作” - Excel task scheduler. I get the error “Microsoft Excel has stopped working” 使用Excel vba宏通过Windows计划任务运行 - Using Excel vba Macro to be run through Windows Schedule Task Excel VBA脚本循环无法正常工作 - Excel VBA script looping not working fine 仅当 Windows 任务计划程序打开文件时才运行 Excel VBA - Run Excel VBA only if Windows task scheduler opened the file 从任务计划程序启动excel工作,现在失败 - Start excel from Task Scheduler was working, now fails 使用windows10任务调度程序运行python脚本,使用win32运行excel文件。 每当用户登录或未登录时 - Using windows10 task scheduler to run python script that uses win32 to run excel file. Whenever user is logged in or not
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM