VBA fails when Task Scheduler is set to “Run wheth

2019-05-13 20:42发布

问题:

I have an Access VBA macro that generates a report, saves it in .pdf and then sends it by e-mail using CDO. Everything works fine if I run it manually or if I set it to be run on Task Scheduler with the security option "Run only when user is logged on". However, if I set the option to "Run whether user is logged on or not" (even with the option "Run with highest privileges") the program crashes on the second line of:

strFileFullPath = CurrentProject.Path & "\Test Report.pdf" DoCmd.OutputTo acOutputReport, strReportName, acFormatPDF, strFileFullPath

with the error

Microsoft Access can't save the output data to the file you've selected.

I am pretty sure that this happens because the macro runs with other user in the background. I have been searching for a solution but all I have found is that it is not possible and that I should change for other printing methods, such as PDF Creator (which brings a lot of other issues).

I am using Access 2016 in Windows Server 2012 R2 Standard.

回答1:

It sounds crazy, but after a few days with no results, I managed to solve my problem just by creating these two empty folders:

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

All the credits to Faye's comment on the bottom of this page: https://blogs.technet.microsoft.com/askperf/2015/02/18/help-my-scheduled-task-does-not-run/

Although the comment regards Excel, it solved my issue on Access. It seems that is related with Office having trouble with running some processes (in my case, a pdf generation) in non-interactive mode (which is the mode that Task Scheduler runs when "Run whether user is logged on or not" is checked).