JonghoKim JonghoKim - 1 year ago 111
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)


End Sub


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.Application.Run("test", some magic needs here)

Answer Source

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 ( 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?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download