2

I have to run macros in an excel file, which can be done through python win32. But I have a third party excel add-in required to run the macros. The excel which is opened through win32 does not have the add-ins linked. The add-in is in a XLA file format.

This is my code

new_file_path = path to excel_file.xlsm
xl = win32com.client.Dispatch('Excel.Application')
xl.Visible = True
refprop_path = path to XLA file
xl.Workbooks.Open(refprop_path)
xl.AddIns.Add(refprop_path)

xl.Workbooks.Open(new_file_path)
xl.Application.Run("iter1.xlsm!Sheet1.copy_data")
for i in range(0, 3):
    xl.Application.Run("iter1.xlsm!Sheet1.temp_const_gauge")
    xl.Application.Run("iter1.xlsm!Sheet1.copy_data")
xl.Application.Save()
xl.Application.Quit()

The add-in as not added and I get an error.

xl.AddIns.Add(refprop_path).Installed = True
  File "<COMObject <unknown>>", line 3, in Add
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Excel', u'Add method of AddIns class failed', u'xlmain11.chm', 0, -2146827284), None)

1 Answer 1

2

I did not have to explicitly write a line to add the add in. I removed that line and the problem was solved.

Working code:

new_file_path = path to excel_file.xlsm
xl = win32com.client.Dispatch('Excel.Application')
xl.Visible = True
refprop_path = path to XLA file
xl.Workbooks.Open(refprop_path)

xl.Workbooks.Open(new_file_path)
xl.Application.Run("iter1.xlsm!Sheet1.copy_data")
for i in range(0, 3):
    xl.Application.Run("iter1.xlsm!Sheet1.temp_const_gauge")
    xl.Application.Run("iter1.xlsm!Sheet1.copy_data")
xl.Application.Save()
xl.Application.Quit()

I seems that the add-in in the XLA file can be used just by keeping it open in the background.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.