title: Task scheduler won't run python script (pywin32) to open Excel. How to get more info from Pywin32
body: <p>I have a Python script running on Windows Server 2008. It opens a spreadsheet and adds a filter row to the spreadsheet, closes it and then zips it up. The main spreadsheet is created using xlwt. I only use Pywin32 because the users want to have a filter row added. </p>
  
  <p>When executing this manually (ie directly via Windows Explorer or command line) it works. However, when firing the script manually through Task Schduler or letting it trigger at the required time, it does not work.  </p>
  
  <p>The 'run as' user is an administrator. I have checked to make sure it runs with the highest privileges. </p>
  
  <p>I captured the error message by piping the output to a log file. The traceback shows:</p>
  
  <pre><code>File "C:\www\..\main\management\commands\excel_writer.py", line 183, in add_filter_control
      xl.Workbooks.Open(file_path)
  File "&lt;COMObject &lt;unknown&gt;&gt;", line 8, in Open
  pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Office Excel', u'Die Open-Methode des Workbooks-Objektes konnte nicht ausgef\xfchrt werden.', u'C:\\Program Files\\Microsoft Office\\OFFICE11\\1031\\xlmain11.chm', 0, -2146827284), None)
  </code></pre>
  
  <p>OK ... this is in German, but it basically says "the workbook open method could not be executed". </p>
  
  <p>However, I can't work out why. Does anyone have any tips on how to find out why?</p>
  
  <p>Cheers</p>
  
  <p>ALJ</p>
  
  <p>Here is some of the code and settings:</p>
  
  <p><strong>Spreadsheet creation code</strong></p>
  
  <pre><code>...
  w = xlwt.Workbook('UTF-8')
      worksheets = {} 
  
      data_formatting_dict = construct_data_formatting()
  
      for sheetref, sheetname, datasource, dataformat_ref in sheetlist:
          worksheets[sheetref] = w.add_sheet(sheetname)
          fetch_row_heights(sheetref, dataformat_ref)
          xls_set_columnwidth(sheetref, dataformat_ref)
          xls_set_main_titles(sheetref, sheetname, dataformat_ref)
          xls_set_titles(sheetref, sheetname, dataformat_ref)
          xls_insert_filter_row(sheetref, dataformat_ref)
          xls_freeze_panes(sheetref, dataformat_ref)
          xls_write_lines(sheetref,dataformat_ref,datasource,pre_title_rows+title_rows+post_title_rows)
  
      filename = fileroot + ".xls"
      archive = fileroot + ".zip"
      filepath = os.getcwd() + "\\" + filename
      w.save(filepath)
  
      add_filter_control(filepath)  &lt;&lt; This is where the filter code is called.
  
      ziparchive(filename, archive)    
      full_archive_ref = save_to_folder + archive
      shutil.copy(archive, full_archive_ref)
  
      os.remove(filename)
      os.remove(archive)
  ...
  </code></pre>
  
  <p><strong>add_filter_control</strong></p>
  
  <pre><code>def add_filter_control(file_path):
      try: 
          from win32com.client import Dispatch
      except:
          return
  
      xl = Dispatch("Excel.Application") 
      xl.Workbooks.Open(file_path) 
      for id, sheetname, source, formatref in sheetlist:
          cellref = format_params[formatref]['filter_range']
          if cellref is not None:
              xl.ActiveWorkbook.Worksheets(sheetname).Range(cellref).AutoFilter(1)
      xl.ActiveWorkbook.Close(SaveChanges=1) # 1 is True, 0 is False
      xl.quit()
  </code></pre>
  
  <p><strong>Task Scheduler Settings</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-16"?&gt;
  &lt;Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"&gt;
    &lt;RegistrationInfo&gt;
      &lt;Date&gt;2010-07-23T21:25:09.4036437&lt;/Date&gt;
      &lt;Author&gt;WIN-1CW6Q4GAAAM\Administrator&lt;/Author&gt;
      &lt;Description&gt;Refreshes the temp tables in the database and generates the summary report spreadsheet.&lt;/Description&gt;
    &lt;/RegistrationInfo&gt;
    &lt;Triggers&gt;
      &lt;CalendarTrigger&gt;
        &lt;StartBoundary&gt;2010-07-23T05:00:00&lt;/StartBoundary&gt;
        &lt;ExecutionTimeLimit&gt;PT1H&lt;/ExecutionTimeLimit&gt;
        &lt;Enabled&gt;true&lt;/Enabled&gt;
        &lt;ScheduleByDay&gt;
          &lt;DaysInterval&gt;1&lt;/DaysInterval&gt;
        &lt;/ScheduleByDay&gt;
      &lt;/CalendarTrigger&gt;
    &lt;/Triggers&gt;
    &lt;Principals&gt;
      &lt;Principal id="Author"&gt;
        &lt;UserId&gt;WIN-1CW6Q4GAAAM\Administrator&lt;/UserId&gt;
        &lt;LogonType&gt;Password&lt;/LogonType&gt;
        &lt;RunLevel&gt;HighestAvailable&lt;/RunLevel&gt;
      &lt;/Principal&gt;
    &lt;/Principals&gt;
    &lt;Settings&gt;
      &lt;IdleSettings&gt;
        &lt;Duration&gt;PT10M&lt;/Duration&gt;
        &lt;WaitTimeout&gt;PT1H&lt;/WaitTimeout&gt;
        &lt;StopOnIdleEnd&gt;true&lt;/StopOnIdleEnd&gt;
        &lt;RestartOnIdle&gt;false&lt;/RestartOnIdle&gt;
      &lt;/IdleSettings&gt;
      &lt;MultipleInstancesPolicy&gt;IgnoreNew&lt;/MultipleInstancesPolicy&gt;
      &lt;DisallowStartIfOnBatteries&gt;true&lt;/DisallowStartIfOnBatteries&gt;
      &lt;StopIfGoingOnBatteries&gt;true&lt;/StopIfGoingOnBatteries&gt;
      &lt;AllowHardTerminate&gt;true&lt;/AllowHardTerminate&gt;
      &lt;StartWhenAvailable&gt;true&lt;/StartWhenAvailable&gt;
      &lt;RunOnlyIfNetworkAvailable&gt;false&lt;/RunOnlyIfNetworkAvailable&gt;
      &lt;AllowStartOnDemand&gt;true&lt;/AllowStartOnDemand&gt;
      &lt;Enabled&gt;true&lt;/Enabled&gt;
      &lt;Hidden&gt;false&lt;/Hidden&gt;
      &lt;RunOnlyIfIdle&gt;false&lt;/RunOnlyIfIdle&gt;
      &lt;WakeToRun&gt;false&lt;/WakeToRun&gt;
      &lt;ExecutionTimeLimit&gt;PT1H&lt;/ExecutionTimeLimit&gt;
      &lt;Priority&gt;7&lt;/Priority&gt;
    &lt;/Settings&gt;
    &lt;Actions Context="Author"&gt;
      &lt;Exec&gt;
        &lt;Command&gt;run_reports.bat&lt;/Command&gt;
        &lt;WorkingDirectory&gt;C:\www\my_site\mysite\&lt;/WorkingDirectory&gt;
      &lt;/Exec&gt;
    &lt;/Actions&gt;
  &lt;/Task&gt;
  </code></pre>