Creating An AddIn
This page has been replaced with a fully updated version.
Click here to go to the new page.
An add-in is a special type of workbook that provides
extended or enhanced functionality to Excel or provides support for a
particular custom application. This page describes standard Excel add-ins,
files with the "xla" filename extension and are loaded through the "Tools"
menu "Add-Ins" dialog. This page does not discuss
COM Add-Ins or Automation
add-ins. There topics are presented elsewhere on the site. The primary differences between Add-In
workbooks and regular workbooks are:
Dim WB As Workbook For Each WB In Workbooks Debug.Print WB.Name Next WB
Debug.Print Workbooks("TestAddin.xla").Name
To create an add-in, open a new, empty workbook and then go to the File menu, choose Properties in the list and enter in the "Subject" box the text that you want to display in the Add-Ins dialog for your add-in. This is unrelated to the file name which you use to save the add-in. This value will be displayed in the Add-Ins dialog Box. Next, enter some descriptive text in the "Comments" box. This text will be displayed in the Description Box of the Add-Ins dialog box. Later, if you wish to change these properties, you must first set the IsAddIn flag using Workbooks("TestAddin.xla").IsAddin = False make the change in the Properties dialog, and then reset the IsAddIn flag with Workbooks("TestAddin.xla").IsAddin = True and finally, save the add-in with Workbooks("TestAddin.xla").Save The commands shown above may be entered directly into the Immediate Window in the VBA Editor. You should also change the name of the project from
the default "VBAProject" to a meaningful name. On the Tools menu in VBA,
choose "VBAProject Properties" to display the Properties dialog, and give
your project a new name. If desired, you can check "Lock Project For
Viewing" and assign a password to protect the code. The Save As dialog will automatically change the destination folder to the AddIns folder of your User Profile directory. You can save the file in that default directory, or in any other folder. Since an Add-In is never visible (unless you clear the IsAddIn flag as described above), you will likely want to include some user elements such as menu items or command buttons to access the features of your add-in (this is not necessary if the sole purpose of the add-in is to provide additional worksheet commands -- in this case, you might want to consider and Automation Add-In, written in VB6). Typically, this is done with a command bar. The following code will create a new command bar with two buttons, one assigned to execute a procedure named "Macro1" and the other assigned to execute a procedure named "Macro2": Private Const C_COMMANDBAR_NAME = "MyAddinCommandBar" Sub Auto_Open() Dim CmdBar As Office.CommandBar Dim CmdBtn As Office.CommandBarButton '''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Delete the command bar. The command bar should not ' exist at this point, but it may not have been ' properly deleted (e.g., system crash) '''''''''''''''''''''''''''''''''''''''''''''''''''''' On Error Resume Next Application.CommandBars(C_COMMANDBAR_NAME).Delete On Error GoTo 0 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Create a new, temporary command bar with a name C_COMMANDBAR_NAME. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Set CmdBar = Application.CommandBars.Add(Name:=C_COMMANDBAR_NAME, _ temporary:=True) ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Create two temporary control buttons on the command bar. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Set CmdBtn = CmdBar.Controls.Add(Type:=msoControlButton, temporary:=True) With CmdBtn .FaceId = 81 .Caption = "Macro 1" .Style = msoButtonIconAndCaption .OnAction = "'" & ThisWorkbook.Name & "'!Macro1" End With Set CmdBtn = CmdBar.Controls.Add(Type:=msoControlButton, temporary:=True) With CmdBtn .FaceId = 80 .Caption = "Macro 2" .Style = msoButtonIconAndCaption .OnAction = "'" & ThisWorkbook.Name & "'!Macro2" End With ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Make the commandbar visible and put it in the docked ' location with other command bars, not a floating ' command bar. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' CmdBar.Visible = True CmdBar.Position = msoBarTop End Sub The command bar is destroyed when the add-in is unloaded (either by closing Excel or unchecking the add-in's entry in the Add-Ins dialog box. Sub Auto_Close() '''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Delete the command bar. '''''''''''''''''''''''''''''''''''''''''''''''''''''' On Error Resume Next Application.CommandBars(C_COMMANDBAR_NAME).Delete On Error GoTo 0 End Sub In the Auto_Open code above, the Face IDs were arbitrarily chosen to be 80 and 81. In your add-in, you will want to use a more appropriate FaceID. John Walkenbach has an excellent add-in that will display all available FaceIDs, displaying the picture along with the FaceID value. Read about it and download it here. As an alternative to a command bar, you could add an item to a menu. The following code will create two items on the Tools menu and assign Macro1 and Macro2 to those menu items. Private Const C_TOOLS_MENU_ID = 30007 Private Const C_TAG = "MyTag" Sub Auto_Open() Dim Ctrl As Office.CommandBarButton '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Set Ctrl as a new control on the Tools menu. The Tools menu is ' found with FindControl using the ID C_TOOLS_MENU_ID value. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Set Ctrl = Application.CommandBars.FindControl(ID:=C_TOOLS_MENU_ID). _ Controls.Add(Type:=msoControlButton, temporary:=True) '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Set the attributes of the control. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' With Ctrl .Tag = C_TAG .Caption = "Macro 1" .BeginGroup = True .FaceId = 80 .Style = msoButtonIconAndCaption .OnAction = "'" & ThisWorkbook.Name & "'!Macro1" End With Set Ctrl = Application.CommandBars.FindControl(ID:=C_TOOLS_MENU_ID). _ Controls.Add(Type:=msoControlButton, temporary:=True) With Ctrl .Tag = C_TAG .Caption = "Macro 2" .BeginGroup = False .FaceId = 81 .Style = msoButtonIconAndCaption .OnAction = "'" & ThisWorkbook.Name & "'!Macro2" End With End Sub In the code above, the BeginGroup property of the first control is set to True to include a separator line above the newly created control. The Tag property can be any unique string. This value is used to find the controls in order to delete them in the Auto_Close procedure, shown below. Sub Auto_Close() '''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Delete the controls. '''''''''''''''''''''''''''''''''''''''''''''''''''''' Dim Ctrl As Office.CommandBarControl Set Ctrl = Application.CommandBars.FindControl(Tag:=C_TAG) Do Until Ctrl Is Nothing Ctrl.Delete Set Ctrl = Application.CommandBars.FindControl(Tag:=C_TAG) Loop End Sub Once you have created your add-in, you need to load it. Save and close your add-in project. Then go to the Tools menu in Excel, choose Add-Ins and in the Add-Ins dialog box, click "Browse" and navigate to and open your add-in. This will permanently add it to the list. That's about all there is to writing an Excel add-in. |
|
|