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