2

The code for python works except that once it has ran I still get a box from excel asking "want to save your changes to workbook" what I seem to be missing in my code is to add something that will SAVE the workbook at the very end. I use RTD functions in the workbook which might be the reason the popup.

This is my python code used.

from __future__ import print_function
import unittest
import os.path
import win32com.client

class ExcelMacro(unittest.TestCase):
    def test_excel_macro(self):
        try:
            xlApp = win32com.client.DispatchEx('Excel.Application')
            xlsPath = os.path.expanduser('C:\Magic Samsung\Watch Samsung\Workbook.xlsm')
            wb = xlApp.Workbooks.Open(Filename=xlsPath)
            xlApp.Run('ArchiveMaster')
            wb.Save()
            xlApp.Quit()
            print("Macro ran successfully!")
        except:
            print("Error found while running the excel macro!")
            xlApp.Quit()
            if __name__ == "__main__":
            unittest.main()
Try
  • 61
  • 1
  • 9

1 Answers1

0

according to this Link xlOpenXMLWorkbookMacroEnabled is 52. so, while saving just give the argument FileFormat = 52 as shown in the below code

class ExcelMacro(unittest.TestCase):
    def test_excel_macro(self):
        try:
            xlApp = win32com.client.DispatchEx('Excel.Application')
            xlapp.DisplayAlerts=False
            xlsPath = os.path.expanduser('C:\Magic Samsung\Watch Samsung\Workbook.xlsm')
            wb = xlApp.Workbooks.Open(Filename=xlsPath)
            xlApp.Run('ArchiveMaster')
            wb.SaveAs(Filename=path_here, FileFormat=52)
            xlApp.Quit()
            print("Macro ran successfully!")
        except:
            print("Error found while running the excel macro!")
            xlApp.Quit()
            if __name__ == "__main__":
            unittest.main()

Note: A pop-up appear's while using saveAs. This SO question and This So Post answer's this problem

rawwar
  • 4,834
  • 9
  • 32
  • 57
  • It does run but at the end I still get the excel popup "want to save your changes to workbook" which I just want to save.... – Try May 31 '18 at 11:49
  • @Try, check the answer, i have updated it with a solution, in short, do wb.DisplayAlerts = False – rawwar May 31 '18 at 11:56
  • at what point in the code would I add wb.DisplayAlerts = False? – Try May 31 '18 at 12:19
  • Try Before saving – rawwar May 31 '18 at 12:20
  • Still getting excel popup "want to save your changes to workbook" :( – Try May 31 '18 at 13:03
  • try with xlapp.DisplayAlerts=False – rawwar May 31 '18 at 14:01
  • just can't seem to finish off the code for the excel popup "want to save your changes to workbook" – Try May 31 '18 at 18:25
  • i am trying it now, i will update the code in another 15 minutes – rawwar May 31 '18 at 18:35
  • @Try, i just did, and i did not get any pop-up – rawwar May 31 '18 at 18:38
  • The new updated one unfortunately didn't run at all. The issue is that the file is saved then application quit but I use an RTD function in excel that updates data in 2 mins intervals so when Python quits excel you get the prompt of either (save, don't save, cancel) somehow I need a code to choose save at this stage. – Try May 31 '18 at 20:39