Khôi Nguyễn
Khôi Nguyễn

Reputation: 1

Macro does not execute completely when using python

When I run macro in Excel, the code executes completely. But the code is not completely done when I try to run macro using Python.

In the VBA code, I used this code to ignore all error triangles in each cell so the file created did not include these errors(image below). enter image description here

But when I try to use macro in Python. The triangle errors occur, and I don't know why (Python code).

import os import win32com.client as wincl

def runMacro(file_path): if os.path.exists(file_path): excel_macro = wincl.DispatchEx("Excel.Application")

    workbook = excel_macro.Workbooks.Open(file_path)#,None,False)

    try:
            excel_macro.Application.Run("module1.create_WP")
            workbook.Save()
    except Exception as ex:
        print(ex)
    finally:
        excel_macro.Application.Quit()
        del excel_macro

path = "D:\OneDrive - Central Group\Khôi\Weekly performance\Original files\TOTAL2022(UPDT.).xlsm" runMacro(path).

The triangle errors occur enter image description here

Upvotes: 0

Views: 274

Answers (1)

ASH
ASH

Reputation: 20342

Can you try it this way and feed back results?

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:\\Excel.xlsb')
            wb = xlApp.Workbooks.Open(Filename=xlsPath)
            xlApp.Run('Macro1')
            wb.Save()
            xlApp.Quit()
            print("Macro ran successfully!")
        except:
            print("Error found while running the excel macro!")
            xlApp.Quit()
if __name__ == "__main__":
    unittest.main()

Upvotes: 0

Related Questions