The This Workbook code module

When the add-in is opened, the Workbook_Open procedure runs. This procedure, which is listed below, adds a menu item to the Tools menu. The menu item contains the text defined by the APPNAME constant, which is declared in Modulel (see the next section).

Private Sub Workbook_Open()

Dim ToolsMenu As CommandBarPopup Dim NewMenultem As CommandBarButton

Set ToolsMenu = Application.CommandBars(1).FindControl(Type:=10, ID:=30007)

Set NewMenultem = ToolsMenu.Controls.Add(Type:=1, Temporary:=True)

With NewMenultem

.Caption = APPNAME .OnAction = "RunTextTools" End With End Sub

When the add-in is closed, the Workbook_BeforeClose procedure is executed. This procedure removes the menu item from the Tools menu.

Private Sub Workbook_BeforeClose(Cancel As Boolean) On Error Resume Next

Application.CommandBars(1).FindControl(Type:=10, ID:=30007).Controls(APPNAME).Delete

On Error GoTo 0 End Sub

Figure 16-4 shows the Tools menu when the Text Tools utility is opened.

Refer to Chapter 23 for more information about adding a menu item to a menu.

0 0

Post a comment