Click Event Is Not Raised for a Custom Word CommandBarButton Object (826931)



The information in this article applies to:

  • Microsoft Office Word 2003
  • Microsoft Visual Studio Tools for the Microsoft Office System version 2003

SYMPTOMS

You can create 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 2003 to uniquely identify the CommandBarButton object. This is particularly useful in Word 2003 because Word 2003 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:
  1. Start Microsoft Visual Studio .NET 2003.
  2. On the File menu, point to New, and then click Project.
  3. 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.
  4. In the Microsoft Office Project Wizard, click Finish.
  5. Add the following class-level variable to the OfficeCodeBehind class:
    Private WithEvents oButton As Office.CommandBarButton
  6. 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 = "My New Button"
    'oButton.Tag = "My New Button Tag"
  7. 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("My New Button click!")
    End Sub
  8. On the Build menu, click Build Solution.
  9. On the Debug menu, click Start.

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

    Note that the Click event of the button does not fire.
  11. To resolve the problem, uncomment the following line of code in the ThisDocument_New event:
    'oButton.Tag = "My New Button Tag"
    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.

Modification Type:MajorLast Reviewed:3/23/2006
Keywords:kbprb kbnofix KB826931 kbAudDeveloper