[python-win32] Excel - accessing a control's properties in an event

Roger Upole rwupole at msn.com
Mon May 12 14:03:01 CEST 2008


Sriram Sundararajan wrote:
>I have a question on the excelAddin.py sample in the win32com\demos folder.
>
> We create a Tool Bar, add a button to it,
> btnMyButton = cbcMyBar.Controls.Add(Type=constants.msoControlButton,
> Parameter="Greetings")
>
> 'register' the button with the ButtonEvent class,
> btnMyButton=self.toolbarButton = DispatchWithEvents(btnMyButton,
> ButtonEvent)
>
> and set a couple of properties.
> btnMyButton.Caption = "&Python"
> btnMyButton.TooltipText = "Python rules the World"
>
> In the OnClick event handler of the ButtonEvent class,
> class ButtonEvent:
>    def OnClick(self, button, cancel):
>
> 1. How is the 'button' argument related to the actual button that was
> clicked? It showed up as a PyIDispatch object when I tried printing
> it.
> 2. Assuming there is more than one button that can  be clicked, how
> would I access some of the properties of the button instance that was
> actually clicked by the user?
>
> Thanks
> Sriram

You can use win32com.client.Dispatch(button) to get a wrapped
object that exposes all the properties of the button.
This will allow you to access the Caption, TooltipText, or any
other property you need to identify the button.

               Roger




More information about the python-win32 mailing list