VBA code inside Excel doesn't run when triggered via Scheduler

a 夏天 提交于 2019-12-12 02:53:55

问题


So the setup on this WinServer 2012 R2 64bit is:

Windows Task Scheduler -> cscript .vbs file -> opening excel and run a sub in the main module

This runs fine in the background when I double click the .vbs file, but when I trigger the .vbs via the task scheduler, excel opens, but doesn't load the file or run the sub (not sure which). The task runs under an domain user that has administration rights on the machine. I use the same user when i try clicking on the .vbs

Code that is being run, in order:

Task scheduler launches:

C:\WINDOWS\system32\cscript.exe "D:\xyz\trigger.vbs"

.vbs does:

Option Explicit

Dim xlApp, xlBook, xlsheets, xlcopy
Set xlApp = CreateObject("Excel.Application")
xlapp.Interactive = False
xlapp.DisplayAlerts = False
xlapp.AskToUpdateLinks = False
xlapp.AlertBeforeOverwriting = False
Set xlBook = xlApp.Workbooks.Open("D:\xyz\excelfile.xlsm")

On Error Resume Next
Call xlBook.Application.Run("Main.Extrnal_Trigger")

xlbook.Saved = True
xlBook.Close
xlApp.Quit

Set xlBook = Nothing
Set xlcopy = Nothing
Set xlApp = Nothing

WScript.Quit(1)

Excel code:

Sub Extrnal_Trigger()

Application.EnableEvents = False
Application.AskToUpdateLinks = False
Application.DisplayAlerts = False
Application.AlertBeforeOverwriting = False

Call update_button
Call MainProgram
Call ReportSave

End Sub

How can I find out where the .vbs or the excel hangs and why? A very similar setup on another machine does run without troubles. It is virtually identical to the code quoted here.

I realize there are several bad practices (like not cleaning up xlapp settings), but I'd like to get the process running before cleaning up.


/edit: Removing

On Error Resume Next

from the .vbs does not display an error.


/edit2: I tried reverting as far back as possible.

Option Explicit
Dim fso, f, s, log

Set fso = CreateObject("Scripting.FileSystemObject")

Set log = fso.CreateTextFile("D:\xyz\TESTlog.txt")
log.WriteLine "before fso"

Set f = fso.GetFile("D:\xyz\excel.xlsm")
s = f.Path & "  "
s = s & "Created: " & f.DateCreated & "  "
s = s & "Last Accessed: " & f.DateLastAccessed & "  "
s = s & "Last Modified: " & f.DateLastModified   

log.WriteLine "after fso"
log.writeline "fso content"
log.writeline s

This works when being triggered by the task scheduler via cscript.exe. I will try to modify to log what's happening around the call to the excel file.


/edit3: Debugging showed that this

Set xlBook = xlApp.Workbooks.Open("D:\xyz\excel.xlsm")

never happens. I put out error numbers and got error 1004 for this call. Still not sure what's the issue, but at least I got an error number now.


/edit4:

error 1004 when trying to run this as a scheduled tasks persists. When I am running it by double clicking the .vbs, everything works.


回答1:


The key was to create both these folders:

C:\Windows\System32\config\systemprofile\Desktop

and

C:\Windows\SysWOW64\config\systemprofile\Desktop

Excel apparently has troubles running in non-interactive mode when these folders are not present (not sure why). Creating them got rid ofthe 1004 error when opening the workbook via vbs.



来源:https://stackoverflow.com/questions/36571522/vba-code-inside-excel-doesnt-run-when-triggered-via-scheduler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!