JonghoKim JonghoKim - 1 month ago 17
Python Question

How to run a vba macro through Python win32com that has IRibbonControl as a parameter

I want to run a vba macro that has IRibbonControl as a parameter using Python win32com.

For example, my vba sub is looks like below

Public Sub test(control As IRibbonControl)

blabla

End Sub


And

And then Python calls test sub using win32com.

What should we put on parameters of a test function?

import os
import win32com.client

from win32com.client import DispatchEx

xl = DispatchEx('Excel.Application')
xl.Workbooks.Open("myexcelfile.xlsm")

xl.Application.Run("test", some magic needs here)

Answer

You would need an object on python-side that implements IRibbonControl, and pass that as parameter back to Excel.

The only way I can imagine this makes sense (but I have an open mind) is if you implement a COM add-in (https://support.microsoft.com/en-us/kb/291392) as IDTExensibility2. Then you will be called the OnConnection method (in your python COM server), which gives you the Excel application object.

From there, you could very likely obtain a reference to the ribbon control.

I must admit that I did so far not manage to get any python COM Excel add-in to work myself, though I tried.

Or can you not better obtain the IRibbonControl from the excel application from within the sub, and remove it from the argument list?