Share via


Creating Add-ins Using Visual Basic

Home Page (Add-ins)OverviewHow Do I ... TopicsFAQReference

In addition to creating add-ins with Visual C++, you can create them with Visual Basic version 4.0 or later. The easiest way to create an add-in with Visual Basic is to start with the sample provided. The sample for Visual Basic version 4.0 is called VB4AddIn, and the sample for Visual Basic versions 5.0 or later is called VB5AddIn.

Add-ins created with Visual Basic version 5.0 (or later) have the following advantages over Visual Basic version 4.0:

  • Your add-in can handle events fired by the Application object. (In the sample code, the m_app member variable points to this object.)

  • The DSAddIn object implements the COM interface IDSAddIn rather than a dispatch interface. This results in faster calls from Developer Studio to your add-in's DSAddIn object.

Creating an add-in with Visual Basic can be divided into the following steps:

  1. Copy the sample files

  2. Name and describe the add-in

  3. Reference the Visual C++ Type libraries

  4. Modify the add-in code:

    • For connecting and disconnecting

    • For the command the add-in will run

    • To add the command to the Visual C++ environment

    • To create event handlers (optional)

  5. Build the add-in .DLL

Optionally, after creating an add-in project, you can employ early binding to optimize the add-in's performance.

Copying the Sample Add-in Files

To copy the sample add-in files

  • In the Visual C++ online documentation, open the topic , and click the link of any sample to copy its project files to a new directory. You can specify the directory.

Naming and Describing the Add-in

To name and describe the add-in

  1. In Visual Basic, open the sample add-in project.

    Note that this project in Visual Basic is a .DLL project type. When you build it, you will build a .DLL.

  2. On the Project Properties dialog box, General tab, give the project a unique name.

  3. On the Project Properties dialog box, Make tab, give the application a unique title.

  4. In the Visual Basic Object Browser, from the Project/Library drop-down, select the add-in project (for example, VB5AddIn).

  5. Select the DSAddIn class, click the right mouse button, and select Properties from the popup menu.

  6. On the Member Options properties page, enter a name in the Description box and click OK.

    Once the add-in project is built, this name appears in Visual C++ on the Add-ins and Macros Files tab of the Customize dialog box (Tools menu, Customize Command).

  7. With the DSAddIn class still selected, click the Description property in the Members list box.

  8. Click the right mouse button and select Properties from the popup menu.

  9. On the Procedure Attributes properties page, enter a description in the Description edit control.

    Once the add-in project is built, this description also appears on the Add-ins and Macros Files tab of the Customize dialog box.

Referencing the Visual C++ Type Libraries

To reference the Visual C++ type libraries

  1. In the Visual Basic References dialog box (Project menu, References command), select the check boxes next to the Visual C++ type libraries.

    For Visual C++ version 5.0, their names begin with "Visual Studio 97." For Visual C++ version 6.0, their names begin with "Visual C++ 6.0."

  2. Click OK.

Modifying the Add-in Code

To customize your add-in, you can modify the code in several ways:

  • Modify the connection and disconnection code

  • Specify the actions the add-in command will perform

  • Add the command to the Visual C++ Developer Studio environment

  • Add event handlers (Visual Basic 5.0 or later)

Some of these steps are optional, or contain optional parts.

Modifying connection and disconnection code

Your add-in will connect to and disconnect from Developer Studio by using the OnConnection and OnDisconnection methods implemented in the DSAddIn class.

  • To view this code in Visual Basic 4.0, look for the connection and disconnection code in the General methods of the DSAddIn class.

  • To view this code in Visual Basic 5.0 or 6.0, look for the connection and disconnection code in the IDSAddIn member of the DSAddIn class.

In the samples, the connection code adds one command to the Visual C++ environment. You add more commands by calling the AddCommand method for each command you want to add (see Adding commands to the Visual C++ environment for details).

In the samples, the disconnection code does nothing; however, you might want to modify it to add clean-up code that destroys the objects you create.

Specifying the actions the add-in command will perform

The Commands class defines the actions performed by the commands you add to the Visual C++ environment. The samples define one action, which is to display a message box.

  • To view or modify this code, open the code editor for the Commands class, and choose the General section.

Adding the command to the Visual C++ environment

This consists of two steps for every command you add: Adding a public procedure to the Commands class, and adding a call to the AddCommand method. Optionally, you can create a toolbar button for the command.

To add a public procedure to the Commands class

  1. Open the code editor for the Commands class, and choose the General section.

  2. From the Tools menu, choose Add Procedure.

  3. Enter a name for the procedure, select the Sub and Public radio buttons, and click OK.

  4. Type the procedure in the code window.

To add a call to the AddCommand method

  • In the OnConnection code, add a call to the AddCommand method. (For more information, see Modifying connection and disconnection code.)

To create a toolbar button for the command

  1. Add an image for the button to the files Tbarmedm.bmp and Tbarlrge.bmp (located in the directory where you copied the add-in sample files).

  2. Reference the bitmap locations in the AddCommand call.

  3. If you want the button to automatically appear on your add-in's toolbar when you first connect the add-in to the Visual C++ environment, in the OnConnection code call the AddCommandBarButton method when the bFirstTime parameter is True.

Add event handlers (Visual Basic 5.0 or later)

  1. Open the code editor for the Commands class, and choose General from the Object drop-down list

  2. For each event replace the MsgBox command with your event-handling code.

Creating the Add-in DLL

To create the add-in DLL

  1. On the File menu, click Make <AddIn_Name>.dll ….

  2. Copy the resulting DLL into the \MSDEV98\AddIns directory.

Now when you return to Visual C++, the add-in appears on the Add-ins and Macros Files tab of the Customize dialog box (Tools menu, Customize Command). You can select it to view its name and description. You can run the add-in by using the toolbar button or key sequence you assign to it. For more information, seeCarrying Out Add-in Commands.

Employing Early Binding

An add-in can employ early binding by using the COM interface. Early binding makes all calls into the interface faster at run time. In Visual Basic, you use early binding by declaring object variables as the appropriate types. For example, the following code accesses text-specific members of an object by declaring the MyTextDoc variable as TextDocument.

Dim MyDoc as Document
set MyDoc = Application.ActiveDocument
if (MyDoc.Type = "Text") then
   Dim MyTextDoc as TextDocument
   ' VB does an implicit QueryInterface
   ' for the following set
   set MyTextDoc = MyDoc
   ' Now, you can use text-specific members of
   ' MyTextDoc
End If