0

I'm using Python to open quite complicated Excel file. I have managed how to open spreadsheet, fill data and run macro. The problem is, that this macro in the end is returning message about result as message box which requires user to click OK button, and will not continue untill button will be pressed. I need to do this process fully automatical, so I need to click 'OK' from Python. How can I do this? Would be great if I could read message from messagebox, but it is not necessary.
Here is my code:

import win32com.client
xl=win32com.client.Dispatch("Excel.Application")
xl.Workbooks.Open(Filename="my_excel.xlsm")
xl.Application.Run("'my_excel.xlsm'!my_macro")

# here I need to click OK, but my code will never reach this place until I'll click it manually

xl.Application.Quit()
vogutodi
  • 11
  • 1

1 Answers1

0

Assuming that you cannot change the actual macro code because it is password protected (you should state that in the first place to avoid confusion), my only suggestion would be to do something rather "analog" like this Controlling mouse with Python

Community
  • 1
  • 1
and0r
  • 305
  • 1
  • 4
  • 13
  • I don't know if this will work because VBA will have control of the machine, right? – sous2817 Feb 08 '17 at 17:09
  • I guess you are right. Two threads would have to be created (http://stackoverflow.com/questions/2905965/creating-threads-in-python) to resolve that. The second one would have to work time-counter based, if there is no other indicator, that the macro has finished. If the macro usually takes 10 minutes, the second thread should start its work after 10 seconds and retry every minute. Or maybe there is a way to listen for system-messagebox-events somehow, but then also event handling would have to be added, too. Ver vague, but OP still hasn't stated yet if he is able to change the macro itself... – and0r Feb 09 '17 at 07:12