Results 1 to 9 of 9
  1. #1
    Vinzafy is offline Novice
    Windows 11 Office 365
    Join Date
    Jan 2023
    Posts
    5

    Post Task Scheduler Headaches for Automated Report PDF Export Script

    Hey all,



    Hoping someone has insight into what is going on with the issue I'm facing.

    Problem

    I have a Python script that exports a report from an Access Database to a PDF file. The script itself works fine when run manually, however when that same exact script is added to Task Scheduler (as a Python or batch file), the script fails due to the following error:

    (-2147352567, 'Exception occurred.', (0, None, "Contact Management Database can't save the output data to the file you've selected.", None, -1, -2146825986), None)

    I don't understand why I can run the script manually and have it work as expected, but when running via Task Scheduler, it gives me that error.

    Script

    import win32com.clientfrom pathlib import Path
    from UtilityBelt import write


    access_db_path = Path(r'<Access Database Path>')


    report_name = r'<Name of Report in Access>'


    report_output = Path(r'<Export folder>\test.pdf')


    if report_output.exists():
    # Deletes existing report if applicable
    report_output.unlink()


    try:
    a = win32com.client.gencache.EnsureDispatch("Access.Ap plication")


    # Open the database
    db = a.OpenCurrentDatabase(access_db_path)


    # Exports the selected report to a PDF
    a.DoCmd.OutputTo(3, report_name, r'PDF Format (*.pdf)', str(report_output))


    a.Application.Quit()


    except Exception as e:
    write(logfile, str(e))

    Relevant Task Scheduler Settings

    General
    • Run whether user is logged in or not
    • Run with highest privileges

    Actions
    • Program/script
      • <path to folder>\access_testing.bat

    • Start in (optional)
      • <path to folder>


    Batch File

    @echo off"<path to python folder>\python.exe" "<path to folder>\access_testing.py"
    pause

    I've tried creating a brand new test access database, logging out then back in, restarting the computer, running in Task Scheduler via Python and Batch, playing with the action parameters, exporting to a local drive as opposed to a network drive, exporting a different file type, running with a different user, ... No luck with anything I've tried.

    Any help would be very much appreciated.

    Thanks all

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,859
    Wouldn't this be better being asked in a Python forum?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    Vinzafy is offline Novice
    Windows 11 Office 365
    Join Date
    Jan 2023
    Posts
    5
    I've tried in many forums and thought I'd reach out here as well.

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,859
    Good luck then.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    The only thing I can think of is Trusted Location or permissions, but you don't say if the TS runs under your profile/login or not.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    Vinzafy is offline Novice
    Windows 11 Office 365
    Join Date
    Jan 2023
    Posts
    5
    Much appreciated!

  7. #7
    Vinzafy is offline Novice
    Windows 11 Office 365
    Join Date
    Jan 2023
    Posts
    5
    Thanks for the reply! TS does run under an admin profile which in theory shouldn't cause permission issues and is set to run with highest privileges.

    I'm really stumped why it works fine when manually kicked off, yet fails via Task Scheduler. White whale of a mystery so far.

  8. #8
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    Still not clear to me if the TS is running under your login or not. If not, maybe one of the sets of permissions is faulty. Did you try running the schedule under your login?
    Could be an IT thing if they've blocked scripts outside of certain parameters.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  9. #9
    Vinzafy is offline Novice
    Windows 11 Office 365
    Join Date
    Jan 2023
    Posts
    5
    2023-01-20 UPDATE

    Turns out it wasn't an issue with permissions, Access, Python, or anything like that. It was an issue with Task Scheduler as I thought, but it turned out to be a much simpler fix than I thought it would be.

    The option of "Run whether a user is logged in or not" was causing the issue. Changing this to "Run only when a user is logged in" fixed the problem.

    This task was running on a dedicated desktop that I would remote into. Despite never logging out when ending a session (just closing the Remote Desktop window), setting the task to "Run only when a user is logged in" fixed the task.

    As the script opens Access and exports a PDF from there - a process that requires a user being logged in - my best guess is that having the option to run whether logged in or not, conflicted with that process and caused the export to fail. Glad that one is solved. Sometimes the fix is a simple one!

    Share
    Edit







Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 6
    Last Post: 12-17-2018, 07:33 PM
  2. Task Scheduler Issue
    By LonghronJ in forum Modules
    Replies: 14
    Last Post: 04-13-2018, 09:03 AM
  3. Windows Task Scheduler .Bat File Macro
    By mrmmickle1 in forum Access
    Replies: 26
    Last Post: 09-08-2015, 04:21 PM
  4. Tricky Relationships in a Task Scheduler
    By igglebop in forum Database Design
    Replies: 2
    Last Post: 05-03-2012, 10:27 AM
  5. Task Scheduler using Service Account
    By ExpertNovice in forum Access
    Replies: 2
    Last Post: 06-11-2010, 02:58 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums