Microsoft KB Archive/826931

= Click event is not raised for a custom Word CommandBarButton object =

Article ID: 826931

Article Last Modified on 5/11/2007

-

APPLIES TO


 * Microsoft Office Word 2007
 * Microsoft Office Word 2003
 * Microsoft Visual Studio Tools for the Microsoft Office System version 2003

-





SYMPTOMS
You can create a Microsoft Office Word 2007 document or a Microsoft Office Word 2003 document that uses a managed code extension. You can create the managed code extension with Microsoft Visual Studio Tools for the Microsoft Office System 2003. The code extension creates a CommandBarButton object in one of the built-in CommandBar objects in Word. When you click this CommandBarButton object, the Click event does not occur in the managed code extension. The result is that you cannot determine when a user tries to access your custom CommandBarButton object.



CAUSE
This problem occurs when the Tag property of the CommandBarButton object is not set to a value.



STATUS
This behavior is by design.



MORE INFORMATION
The Tag property permits Microsoft Office to uniquely identify the CommandBarButton object. This is particularly useful in Word because Word uses Single Document Interface (SDI). SDI permits each document window to have its own set of commandbars. Therefore, Microsoft recommends that you always set the Tag property to a unique string for each CommandBarButton object that you create. This is especially important when you add your own CommandBarButton object to existing Word command bars such as menus, context menus, and toolbars.

Steps to reproduce the problem
The following steps create a new CommandBarButton object from the Tools menu in Word. To see that the Click event does not occur unless you set the Tag property of the CommandBarButton object, follow these steps:  Start Microsoft Visual Studio .NET 2003. On the File menu, point to New, and then click Project. In the Project Types list, click Microsoft Office System Projects, and then click Visual Basic Projects. In the Templates list, click Word Template. Name the new project WordCommandBarTest, and then click OK.

The Microsoft Office Project Wizard appears. In the Microsoft Office Project Wizard, click Finish.  Add the following class-level variable to the OfficeCodeBehind class. Private WithEvents oButton As Office.CommandBarButton   Add the following code to the ThisDocument_New event handler. ' CommandBar change applies only to the new document. ThisApplication.CustomizationContext = ThisDocument

' Obtain the Tools menu for Word. Dim toolsMenu As Office.CommandBarPopup = _ CType(ThisApplication.CommandBars.FindControl(, 30007), _   Office.CommandBarPopup)

' Add your new item to the Tools menu. oButton = _ CType(toolsMenu.Controls.Add(Office.MsoControlType.msoControlButton), _   Office.CommandBarButton) oButton.Caption = &quot;My New Button&quot; 'oButton.Tag = &quot;My New Button Tag&quot; </li>  Add the following function to the OfficeCodeBehind class. Private Sub oButton_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles oButton.Click MessageBox.Show(&quot;My New Button click!&quot;) End Sub </li> On the Build menu, click Build Solution.</li> On the Debug menu, click Start.

Word starts, and a new document is created from the template.</li> On the Tools menu in Word, click My New Button.

Note that the Click event of the button does not fire.</li>  To resolve the problem, uncomment the following line of code in the ThisDocument_New event. 'oButton.Tag = &quot;My New Button Tag&quot; Now when you quit Word and then you run the solution again, you notice that the Click event of the My New Button fires as you expect. </li></ol>

Additional query words: WD2003 WD2007

Keywords: kbprb kbnofix KB826931

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.