CommandBarButton: Object and Instance

Add-in Express™ Support Service
That's what is more important than anything else

CommandBarButton: Object and Instance
How to enable a particular button instance in MSWORD COM add-in? 
Vladimir Chtchetkine


Guest


Hi everyone:

I have a ATL COM add-in to MSWORD that adds a toobar+button. I've hooked up to the button successfuly (i.e. I catch "clicks" for all buttons in all Word windows and can distinguish between button instances) but I have no idea how can I enable/disable particual button instance without other instances of that button in other windows automatically inherit that changed status. In other words what can I do to enable that particual button [instance] only in that particular Word window while same buttons in other Word windows will keep their statuses?
Posted 22 Sep, 2004 18:43:38 Top
Dmitry Kostochko


Add-in Express team


Posts: 2731
Joined: 2004-04-05
Hello Vladimir,

Unfortunately you can do nothing. Since MS Word has Application.CommandBars property and ActiveDocument.CommandBars property they would seem to be different things. But our tests showed that it isn't so. When you change Enabled property of any Word command bar control in one document (window) it is changed automatically in all other opened documents.

You can see it yourself looking at and trying the code below:

Public Sub AddButton()
  Dim b As CommandBarButton

  Set b = Application.ActiveDocument.CommandBars("Standard").Controls.add(msoControlButton, , , , True)

  b.Style = msoButtonCaption
  b.Caption = "Test Button"
  b.Tag = "TAG_TEST_BUTTON"
End Sub

Public Sub EnableButton()
  Dim b As CommandBarButton

  'Set b = Application.CommandBars.FindControl(Type:=msoControlButton, Tag:="TAG_TEST_BUTTON")
  Set b = Application.ActiveDocument.CommandBars.FindControl(Type:=msoControlButton, Tag:="TAG_TEST_BUTTON")
  b.Enabled = Not b.Enabled
End Sub

Public Sub EnableStandardButton()
  Dim b As CommandBarButton
  
  ' 113 - Bold
  Set b = Application.ActiveDocument.CommandBars.FindControl(Type:=msoControlButton, ID:=113)
  b.Enabled = Not b.Enabled
End Sub


Sincerely,
ADX Support Team
Posted 23 Sep, 2004 05:49:13 Top
Eugene Starostin


ADX Team
Add-in Express team


Posts: 430
Joined: 2004-04-05
Dmitry,

Your answer applies to a common case for Word 2000 and higher. However our customers would be interested in getting the solution for particular versions, e.g. 2002 and 2003. Are there such solutions?
Posted 23 Sep, 2004 06:29:53 Top
Dmitry Kostochko


Add-in Express team


Posts: 2731
Joined: 2004-04-05
Eugene:
No, unfortunately there aren't. There are only slight changes in Office Object Model 2002/2003 related to command bars and command bar controls.

Vladimir:
You can try to trap the WindowActivate event and change the state of your button depending on the window or the document.

Sincerely,
ADX Support Team



Posted 23 Sep, 2004 07:33:31 Top
Vladimir Chtchetkine


Guest


Thank you, Dmitry. Yes, I realized that somehow each button (and, probably, toolbar as well, custom at least) in each Word window represent "one and only one" button object (probably within _Application space). However there is a notion of InstanceID (by which I can distinguish "clicks" in different windows). So, one way or another that "single rooted" button gets "re-instantiated" for each window. Plus (as you can see yourself) Word is capable of disabling a button in one window while keeping the same button enabled in another window... I'll keep searching for answers and will share (if there are any ;-)
As per your window activate/deactivate suggestion - it's workable (in fact I've done that already) but it's "distasteful" :-) and I hate these types of solutions. But I'm mixing personal and professional here and digress;-)
Anyway, thanks for your responses
Cheers,
Vladimir
Posted 23 Sep, 2004 08:54:14 Top