vbams-accessscheduled-tasksms-access-2016windows-task-scheduler

VBA fails when Task Scheduler is set to "Run whether user logged on or not"


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.


Solution

  • I managed to solve my problem by creating these two empty folders:

    All the credits to Faye's comment from 2016-03-17 on the bottom of this blog post: https://web.archive.org/web/20161007034438/https://blogs.technet.microsoft.com/askperf/2015/02/18/help-my-scheduled-task-does-not-run/:

    [...]
    I had the same issue and fought with it for a few months. Finally resorted to calling Microsoft for some assistance. The fix isn’t something I would have ever found, so hopefully this helps others too. The script would run fine when I launched it in Powershell[sic] (and command line) but when it launched as a scheduled task it would run but not with expected results. Microsoft informs me that my issue is with running Excel in “non-interactive mode”, you require two directories to be created.

    You have to create a folder (or two on a 64bit-windows):
    (32Bit, always) C:\Windows\System32\config\systemprofile\Desktop

    (64Bit) C:\Windows\SysWOW64\config\systemprofile\Desktop

    The MS Excel when you are trying to run in non-interactive mode will look for this, if it is not present it will not open in the non-interactive mode.

    once these were created the scheduled task runs flawlessly.

    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).