0

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)
JonghoKim
  • 1,965
  • 7
  • 21
  • 44
  • 1
    I think you're going to find that difficult - if you have control of the VBA it would be best to break the functionality out of the control handler into a separate sub which you can call using `application.Run` – Tim Williams Oct 12 '16 at 05:17

2 Answers2

0

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?

Dr. V
  • 1,747
  • 1
  • 11
  • 14
0

Usually that argument is to capture customized Ribbons calls, incoming from the ribbon. If you are using Python, then you are not using Ribbon, and then you can bypass that, this does not apply to all use cases, although it applies to mine and most also.

Public Sub test(Optional control As IRibbonControl)

    blabla

End Sub

Simply using the argument as optional could help there.

import os
import win32com.client

from win32com.client import DispatchEx

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

xl.Application.Run("test")