3

Background: I am on Windows 7 and am trying to automate some email-related list updates. I have a python script that pulls-in emails, downloads certain attachments and sends me back a new email with some results. This all works great when I run the code manually:

  • via Windows command prompt.
  • Within my IDE.
  • Wrapping it in a .bat file and dbl-clicking the bat file.

To be clear here: When executed in any of the above ways, the script successfully checks emails, parses the attachments and sends an email to myself containing the desired information.

Problem: When I try to set-up a scheduled task (I have admin privileges) on my local PC to run the .bat file, the script terminates at the following lines:

#Grab an outlook COM    
outlook_app = win32com.client.Dispatch("Outlook.Application")
outlook = outlook_app.GetNamespace("MAPI")

I know this because I have the script printing logs to a separate text file. The task scheduler executes the batch file, I see logs being written to the text file for earlier steps, but it fails at these two lines.

Is there something I am missing about win32com and the task scheduler?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Paco
  • 443
  • 3
  • 10
  • Does this answer your question? [.net application failing when fired via scheduled task](https://stackoverflow.com/questions/17913670/net-application-failing-when-fired-via-scheduled-task) – TylerH Jan 19 '23 at 22:23

1 Answers1

0

In order to run tasks using COM Interop, it is necessary to operate with the user logged on.

See these articles.

.net application failing when fired via scheduled task

Schtasks

Create a scheduled task that runs on particular user login using commandline

Office Interop run via Task Scheduler

A workaround for the fact that a scheduled task in Windows requires a user to be logged in

kunif
  • 4,060
  • 2
  • 10
  • 30
  • I am logged-on when trying to execute the scheduled task. I'm going to the Task Scheduler and manually (R-click -> Run) running the scheduled task. – Paco Aug 30 '19 at 23:49
  • So could you send and receive emails? If you're able to handle it, but something else happens, please add details about the situation to the question article or ask a new question. – kunif Aug 31 '19 at 00:34
  • I can send/receive emails. I've updates my original question. The script works fine when run manually - the problem only occurs when task manager executes the script. It does run the initial few lines that print logging information to a local text file (that's how I know it gets that far), but it fails at the above printed lines (the log file doesn't contain entries written immediately after these two lines). – Paco Sep 03 '19 at 12:51
  • If you add the details of the configuration information to the task scheduler where the problem occurred, you will likely get an answer. otherwise, Are there any other records related to event logs, etc.? – kunif Sep 03 '19 at 12:54