1

I have the following code to run a macro, and it works absolutely great when I run it from within python

try:
        xl = win32com.client.Dispatch("Excel.Application")
        xlsPath = os.path.expanduser("C:/Users/hidden/Desktop/scripts/someFile.xlsm")
        wb = xl.Workbooks.Open(Filename=xlsPath)
        xl.Application.Run("GetData")
        xl.DisplayAlerts = 0
        wb.Save()
        xl.Quit()
        del xl

    except Exception:
        xl.Quit()

Now, the script is much more than this and I need it to run as a scheduled task(or similiar) once a day on a server. I've created a scheduled task, which works whether the user is logged on or not and that works as expected as well, but at the point where it executes the macro, it just throws an exception and nothing happens as if it doesn't start the macro at all. I can confirm that it executes until opening the workbook

The task that I have scheduled is a .bat file that contains the following, and as expected all paths required in the script are absolute paths

@echo off
python C:\Users\hidden\Desktop\scripts\automateMacros.py %*

Also, when I double click the bat file, it works as expected, but won't work as scheduled task but i can confirm it get's executed

EDIT: Exception error:

(-2147352567, 'Exception occurred.', (0, u'Microsoft Excel', u"Microsoft Excel cannot access the file 'C://Users/hidden/Desktop/scripts/someFile.xlsm'. There are several possible reasons:\n\n\u2022 The file name or path does not exist.\n\u2022 The file is being used by another program.\n\u2022 The workbook you are trying to save has the same name as a currently open workbook.", u'xlmain11.chm', 0, -2146827284), None)

I only open the file after an if statement that it exists, it's the first workbook accessed. Note also, if I run the batch script straight away again it works as expected

Community
  • 1
  • 1
Slavi
  • 120
  • 4
  • 15
  • Try giving the full path of the python in the batch file. for eg: `C:\Python27\Python.exe C:\Users\hidden\Desktop\scripts\automateMacros.py %*` – ρss Sep 10 '15 at 07:28
  • Hey pss, i did but still writes an error to a log file ;( – Slavi Sep 10 '15 at 08:01
  • Could you please mention the error? – ρss Sep 10 '15 at 08:04
  • I am sorry, i thought I did add it already, did now look at edit =] – Slavi Sep 10 '15 at 08:20
  • I think you have an issue with the scheduled task. Could you please confirm that the task is being executed after some particular time. For eg: the problem could be that when your task executed for the first time. maybe the OS is still writing the excel file or has not closed it completely. And during this time you task executes again and thus the error. Try executing the task with some big time differences. Is the path ok? In error log `C://Users/hidden/Desktop/scripts/someFile.xlsm` there are `\\` & `\`.. Maybe thats the problem – ρss Sep 10 '15 at 08:35
  • The task executes since it writes to the log file the exception. The printed path is with double slashes, I've tried having it like that but didn't help, i also tried having the entire path as back slashes (for windows) and didnt work either same error. The most weird thing is that it works out of the box if i double click the batch file .. – Slavi Sep 10 '15 at 08:40
  • The Solution is found here .... [link][1] [1]: http://stackoverflow.com/questions/4803850/win32com-excel-django-apache-problem – Slavi Sep 10 '15 at 09:37

2 Answers2

0

This link will help you some. It describes how to run vbs and excel macros from windows task scheduler. In addition to this, try vbs instead of python to find if it is a python problem OR try different user to run scheduled task OR try to give a different file name while you are saving then rename it as you want.

Community
  • 1
  • 1
Ozan
  • 1,044
  • 1
  • 9
  • 23
0

Apprently the issue is windows related, here's the solution

link

Slavi
  • 120
  • 4
  • 15