Attribute VB_Name = "modSharedAddInCode" Option Explicit ' Global variable to store reference to host. Public gobjVBE As VBIDE.VBE ' Public constants for menu item in Office application. Public Const CBR_NAME As String = "Add-Ins" Public Const CTL_CAPTION As String = "&List Box Wizard" Public Const CTL_KEY As String = "ListBoxWizard" Public Const CTL_NAME As String = "List Box Wizard" ' Constants for characters surrounding ProgID. Public Const PROG_ID_START As String = "!<" Public Const PROG_ID_END As String = ">" Sub AddInErr(errX As ErrObject) ' Displays message box with error information. Dim strMsg As String strMsg = "An error occurred in the COM add-in named '" _ & App.Title & "'." & vbCrLf & "Error #:" & errX.Number _ & vbCrLf & errX.Description MsgBox strMsg, , "Error!" End Sub Function CreateAddInCommandBarButton(ByVal Application As Object, _ ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, _ ByVal AddInInst As Object) As Office.CommandBarButton ' This procedure assigns a reference to the Application ' object passed to the OnConnection event to a global ' object variable. It then creates a new command bar ' button and returns a reference to the button to the ' OnConnection event procedure. The advantage to ' putting this code in a public module is that if you ' have more than one add-in designer in the project, you can ' CallFrom this procedure from each of them rather than ' duplicating the code. Dim cbrMenu As Office.CommandBar Dim ctlBtnAddIn As Office.CommandBarButton On Error GoTo CreateAddInCommandBarButton_Err ' Return reference to Application object and store it in public variable ' so that other procedures in add-in can use it. Set gobjVBE = Application ' Return reference to command bar. Set cbrMenu = gobjVBE.CommandBars(CBR_NAME) ' Add button to CallFrom add-in from command bar, if it doesn't ' already exist. ' Constants are declared at module level. ' Look for button on command bar. Set ctlBtnAddIn = cbrMenu.FindControl(Tag:=CTL_KEY) If ctlBtnAddIn Is Nothing Then ' Add new button. Set ctlBtnAddIn = cbrMenu.Controls.Add(Type:=msoControlButton, _ Parameter:=CTL_KEY) ' Set button's Caption, Tag, Style, and OnAction properties. With ctlBtnAddIn .Caption = CTL_CAPTION .Tag = CTL_KEY .Style = msoButtonCaption ' Use AddInInst argument to return reference ' to this add-in. .OnAction = PROG_ID_START & AddInInst.ProgID _ & PROG_ID_END End With End If ' Return reference to new command bar button. Set CreateAddInCommandBarButton = ctlBtnAddIn CreateAddInCommandBarButton_End: Exit Function CreateAddInCommandBarButton_Err: ' CallFrom generic error handler for add-in. AddInErr Err Resume CreateAddInCommandBarButton_End End Function Function RemoveAddInCommandBarButton(ByVal _ RemoveMode As AddInDesignerObjects.ext_DisconnectMode) ' This procedure removes the command bar button for ' the add-in if the user unloaded the add-in. On Error GoTo RemoveAddInCommandBarButton_Err ' If user unloaded add-in, remove button. Otherwise, add-in is ' being unloaded because application is closing; in that case, ' leave button as is. If RemoveMode = ext_dm_UserClosed Then On Error Resume Next ' Delete custom command bar button. gobjVBE.CommandBars(CBR_NAME).Controls(CTL_NAME).Delete On Error GoTo RemoveAddInCommandBarButton_Err End If RemoveAddInCommandBarButton_End: Exit Function RemoveAddInCommandBarButton_Err: AddInErr Err Resume RemoveAddInCommandBarButton_End End Function