Creating a Basic Add-In

Building an add-in consists mainly of creating a class module that handles events specific to add-ins and any events you want to specify, along with your support modules and forms. Unless you need to have multiple instances of an add-in running in the same IDE, all class procedures should be declared Private to prevent other routines from inadvertently referencing them.

Once the add-in code is complete, you must compile it as an ActiveX .dll or .exe file, since add-ins must be ActiveX components. For various reasons, it's generally best to create add-ins as ActiveX .dll files.

For more information   For a brief comparison of add-ins as .exe and .dll files, see "Compiling Add-Ins" in "How to Build an Add-In."  get your feet wet, start by building a very simple add-in that demonstrates an essential component of an add-in — its Class module. How this example works will be explained afterward.

Note   The procedure below leads you manually through creation of an add-in. Alternatively, you can simply create an Add-In project, and all of the basic add-in infrastructure is created for you.

To create the AddInProject add-in

  1. On the File menu, click New Project to open the New Project dialog box. (This will close your current project or project group; you will be prompted to save any changes you have made.) Double-click the ActiveX DLL icon to create a new project.

  2. Click the Project menu, then click References. Select the Microsoft Visual Basic Extensibility and Microsoft Office 8.0 Object Library check boxes.

    This gives you access to the extensibility objects and collections that you need to create add-ins.

  3. Click the Project menu, then click Add Module. In the Add Module dialog box, double-click the Module icon to create a new module.

    Note that there is an icon for an AddIn template. The template includes some of the code necessary for beginning an add-in.

    Enter the following code in the new module:

    Declare Function WritePrivateProfileString& Lib _ 
    "kernel32" Alias "WritePrivateProfileStringA" _ 
    (ByVal AppName$, ByVal KeyName$, ByVal _ 
    keydefault$, ByVal FileName$)
    Sub AddToINI()
    Dim rc As Long
    rc = WritePrivateProfileString("Add-Ins32", _ 
    "AddInProject.AddInClass", "0", "VBADDIN.INI")
    MsgBox _ 
    "Add-in is now entered in VBADDIN.INI file."
    End Sub
    

    Alternatively, you can use the Add-In designer to create a reference to your add-in. For detailed instructions about how to do this, see "Referencing Add-Ins" in Chapter 4, "Connecting and Exposing Add-Ins".

  4. In the Project Explorer window, double-click the Class module Class1 to bring it to the front. Set its Name property to "AddInClass" and make sure that its Instancing property is set to "5 – MultiUse".

  5. Add the following line of code to the Class module:
    Implements IDTExtensibility
    

    This adds a reference to the IDTExtensibility object to your project.

  6. Click IDTExtensibility in the Object box.

    Notice that four new events appear in the Procedure box: OnConnection, OnDisconnection, OnStartupComplete, and OnAddInsUpdate.

  7. Click each of the events in the Procedure box to add their procedures to your Class module.

    While you can enter the procedure syntax manually, it's strongly recommended that you click the event name in the box to add the procedures to the Class module to ensure that all names and arguments are entered correctly. Plus, it's faster!

    All four of these event procedures must be present in your Class module for add-ins to work correctly. Also, if one or more of the event procedures has no code in it, it will be removed upon compilation, so it's important that you add at least a comment to each of the four events to ensure that they remain in the Class module when you compile.

  8. Now that you have all four events added, add the following code to them:
    Private Sub IDTExtensibility_OnConnection(ByVal _ 
    VBInst As Object, ByVal ConnectMode As _ 
    VBIDE.vbext_ConnectMode, ByVal AddInInst As _ 
    VBIDE.AddIn, custom() As Variant)
    MsgBox "Add-in is now connected"
    End Sub
    
    Private Sub IDTExtensibility_OnDisconnection(ByVal _ 
    RemoveMode As VBIDE.vbext_DisconnectMode, _ 
    Custom () as Variant)
    MsgBox "Add-in is now disconnected"
    End Sub
    
    Private Sub IDTExtensibility_OnStartupComplete _ 
    (custom() As Variant)
    ' Comment to prevent procedure from being
    ' deleted on compilation.
    End Sub
    
    Private Sub IDTExtensibility_OnAddInsUpdate _ 
    (custom() As Variant)
    ' Comment to prevent procedure from being 
    ' deleted on compilation.
    End Sub
    
  9. On the Project menu, click Project1 Properties, then enter AddInProject in the Project Name box. Click OK.

  10. On the File menu, click Save Project to save the project files. Name them as shown in the following table. Visual Basic will provide the indicated extensions automatically.
    File File name Extension
    Basic module AddIn .bas
    Class module AddInClass .cls
    Project AddInProject .vbp

To test the AddInProject add-in

  1. Click the File menu, then click Make AddInProject.dll. In the Make Project dialog box, click OK.

    This will register the add-in in the system registry.

  2. In the Immediate window, enter AddToINI and press RETURN. You get a message box that says "Add-in is now entered in VBADDIN.INI file."

  3. On the File menu, click New Project to open the New Project dialog box. (This will close AddInProject.vbp; you will be prompted to save any changes.) Double-click the Standard EXE icon to create a new project.

  4. Click the Add-Ins menu, then click Add-In Manager. Notice that a new entry, "AddInProject.AddInClass," appears in the list.

  5. Select AddInProject.AddInClass check box, then click OK.

    At this point, you should get the following dialog box:

  6. Click OK. Clear the AddInProject.AddInClass check box, then click OK.

    You should get the following dialog box:

What Just Happened?

The purpose of this add-in is to demonstrate the components and behavior of an essential component of an add-in — its Class module. Let's go through each of the previous steps and discuss what it did, and more importantly, how and why it was done the way it was.