Partager via


Visual Basic Concepts

Referencing Add-Ins

Once an add-in is registered in the Windows system registry, you must set up a reference to it before it appears in the Add-In Manager and becomes useable to Visual Basic. There are two ways to do this:

  • Create an entry in the [Add-Ins32] section of the Vbaddin.ini file.

  • Create a reference with the Add-In designer.

Creating an Entry in the Vbaddin.ini File

You can create the entry any way you like, but the most often used programmatic method is through the Windows API function WritePrivateProfileString. Here is example code demonstrating how to do this (the function's arguments are described after the example):

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", _
   "MyAddInProject.MyAddInClass", "0", _
   "VBADDIN.INI")
   MsgBox "Add-in is now entered in VBADDIN.INI file."
End Sub

What Do the Arguments Mean?

The first argument of WritePrivateProfileString is the name of the section of the .ini file in which to place the information. In the case of add-ins, this should always be "Add-Ins32".

The second argument is the programmatic ID, which consists of the names of your project and class, separated by a dot. The project name is entered in the Properties dialog box in the Projects menu. The class name is entered in the Name property of the class module. (Note that the name of the module plays no part in this.)

The third argument is a value for the add-in’s entry in the Vbaddin.ini file — in this case, 0. Setting the entry to 1 means that the add-in will be loaded (that is, selected in the Add-In Manager) at IDE startup. Setting the entry to 0 means that it will not be loaded at IDE startup, but it will be included in the list of available add-ins in the Add-In Manager.

The fourth and final argument is the name of the file to apply the setting to. In the case of add-ins, this is the Vbaddin.ini file.

A More Friendly Name

While the name format of project.class (its programmatic ID) looks fine in the .ini file, it might be a bit confusing for users seeing it in the Add-In Manager’s add-in list. You can change this to a more friendly name.

To change the add-in name that appears in the Add-In Manager

  1. In Visual Basic, click the View menu, then click Object Browser.

  2. In the Classes list, right-click the name of the class which handles your add-in’s OnConnection and OnDisconnection events, then click Properties.

  3. In the Member Options dialog box, enter your friendly name in the Description box. Click OK.

The text you enter will be saved along with the add-in, so users will see the same description in their Add-In Manager once it’s installed on their system.

Creating a reference with the Add-In designer

Another (and much easier) way to create a reference to an add-in is with the Add-In designer. To do this:

  1. Create a new Add-In project (if you haven't done so already),

  2. Double-click the Add-In designer named "Connect" in the Project Explorer window. This opens the Add-In designer.

  3. In the Addin Display Name box, type the name of your add-in as you want it to appear later appear in the Add-In Manager dialog box.

  4. In the Addin Description box, type a description of what the add-in does.

  5. In the Application and Application Version boxes, choose the host application and its version, in this case, Visual Basic version 6.0.

  6. In the Initial Load Behavior box, choose how you want your add-in to load:

    • Command Line / Startup: Add-in loads either when specifically invoked from a command-line parameter, or when Visual Basic starts.

    • Command Line: Add-in loads only when specifically invoked from a command-line parameter.

    • Startup: Add-in loads when you start Visual Basic.

    • None: Add-in doesn't load when you start Visual Basic.

  7. Check Addin is command-line safe (Does not put up any UI) if you wish your add-in to run without displaying any dialog boxes or forms—that is, run "behind the scenes" without any visible interface.

  8. Select the Advanced tab to enter additional information on localization resources, and registry settings, if applicable.

Once the above information is filled out and you compile your project, your new add-in will appear in the Add-In Manager's list of available add-ins.