Implementing the Extensibility Model in a Practical WayNow that you know a little more about the Extensibility Model, it would be a good idea to exercise that knowledge by creating a simple Add-In. In this section, you'll build a sample Visual Basic Add-In that will count the number of lines of code for a given program component. To begin creating the sample Add-In, start a new project. Choose the AddIn project type. The AddIn project type includes many components necessary for creating Visual Basic Add-Ins. There is a form (frmAddIn) that you can modify to provide a user interface for your Add-In. There's also a Designer module (Connect) that contains the four methods (AddToAddInCommandBar, Hide, Show, and IDTExtensibility_OnStartupComplete) that are needed for the Add-In's interface to Visual Basic. In this sample, you won't have to worry about any of the project's components except the form and the Connect module. You'll change it so that the user can enter the name of a component (and the project to which it belongs) that will have its number of code lines counted. Display the form on the screen. It should look like the one shown in Figure 31.2. The form for the AddIn project before it is modified to fit the purposes of the sample Add-In. Notice that two CommandButton controls are included on the form for you. We won't be using them, so go ahead and remove them from the form. To build the Add-In's user interface, use Figure 31.3 and Table 31.4 as a guide for adding the different controls and changing their properties. Table 31.4. The controls and their properties that make up the sample Add-In's user interface |
Control Type |
Property |
Value |
Form | Name | frmAddIn |
Caption | Code Line Counter | |
Height | 3480 | |
StartUpPosition | 2 Center Screen | |
Width | 3825 | |
Label | Name | lblProject |
Caption | Project | |
Height | 255 | |
Left | 360 | |
Top | 240 | |
Width | 1695 | |
TextBox | Name | txtProject |
Height | 315 | |
Left | 240 | |
Text | (Nothing) | |
Top | 480 | |
Width | 3135 | |
Label | Name | lblComponent |
Caption | Component | |
Height | 255 | |
Left | 360 | |
Top | 1080 | |
Width | 1695 | |
TextBox | Name | txtComponent |
Height | 315 | |
Left | 240 | |
Text | (Nothing) | |
Top | 1320 | |
Width | 3135 | |
CommandButton | Name | cmdCountCodeLines |
Caption | Count Code Lines | |
Height | 375 | |
Left | 240 | |
Top | 2040 | |
Width | 1695 | |
CommandButton | Name | cmdDone |
Caption | Done | |
Height | 375 | |
Left | 240 | |
Top | 2520 | |
Width | 1695 | |
Label | Name | lblCodeLines |
Alignment | 2 Center | |
Caption | Code Lines | |
Height | 255 | |
Left | 2400 | |
Top | 2280 | |
Width | 855 | |
TextBox | Name | txtCodeLines |
Alignment | 2 Center | |
Enabled | False | |
Height | 315 | |
Left | 2400 | |
Text | (Nothing) | |
Top | 2520 | |
Width | 855 |
Figure
31.3 The user interface (frmAddIn) for the sample Add-In. Next, you need to add a few short event procedures to frmAddIn. Listing 31.1 shows these procedures. Listing 31.1. - The code procedures for the sample Add-In, which should be added to the frmAddIn form. |
Private Sub cmdCountCodeLines_Click() Dim strVBProject As String Dim strVBComponent As String Dim objVBComponent As VBComponent ' Get the Project name and Component name from the ' form's two TextBox controls. strVBProject = txtProject.Text strVBComponent = txtComponent.Text ' Set objVBComponent to the program component suggested ' by strVBProject and strVBComponent. Set objVBComponent = VBInstance.VBProjects.Item(strVBProject).VBComponents.Item(strVBComponent) ' Assign the number of lines of code (CountOfLines) of the ' component objVBComponent to the txtCodeLines TextBox. txtCodeLines.Text = Str(objVBComponent.CodeModule.CountOfLines) End Sub Private Sub cmdDone_Click() ' Hide the Add-In window. Connect.Hide End Sub
Before going any further, let's take a closer look at the two event procedures you just added. The first, cmdCountCodeLines_Click, is triggered when the user clicks on the Count Code Lines button. It uses the project and component names that were typed into the form's TextBox controls (txtProject and txtComponent) to assign that component to the objVBComponent object. Note the hierarchy used to obtain a component item in the following line of code: |
Set objVBComponent = VBInstance.VBProjects.Item(strVBProject).VBComponents.Item(strVBComponent)
First, the VBInstance object is referenced
and then its VBProjects collection. The string value strVBProject included in the first
set of parentheses indicates the project name, which is used as the key argument for the
VBProjects collection's Item method. After the project has been referenced from the VBProjects collection, that project's VBComponents collection is accessed by using the strVBComponent string as a key argument for the collection's Item method. This long sequence of referencing ultimately assigns the specified component (strVBComponent) that is part of the specified project (strVBProject) to the objVBComponent object. The following line of code txtCodeLines.Text = Str(objVBComponent.CodeModule.CountOfLines) is used to access the CodeModule of the newly assigned objVBComponent object. The CountOfLines property of the CodeModule object contains the number of lines of code in that particular component. This number is assigned to the txtCodeLines TextBox so that the user can see the results. The second event procedure that was added is the cmdDone_Click event. This contains only a single line of code that calls the Connect object's Hide method, hiding the Add-In's user interface. The Connect object is an instance of the Connect class, which, as you might remember, is a part of the AddIn project. It is defined in the form's General Declarations section. You can remove the Click events for the CancelButton and OKButton objects because they were deleted earlier. These event procedures are no longer valid because they will never be triggered. The last change that you need to make is a small one, and project will be finished. Bring up the code in the Connect Desiger. In the AddInInstance_OnConnection procedure, there is a line of code that looks like this: Set mcbMenuCommandBar = AddToAddInCommandBar("My AddIn") Change the "My AddIn" to "Code Line Counter". If you don't, then the Add-In will appear on VB's Add-In menu as "My AddIn", which isn't very descriptive. That's all there is to the sample Add-In. However, you need to do something before you save the project. Choose Project MyAddIn Properties from the menu. Change the project name from "MyAddIn" to something more appropriate, such as "CodeLineCounter". Then click OK. Now you can save the project. Save the form as CLCFORM.FRM, the designer as CLCCONNECT.DSR, and the project as CODELINECOUNTER.VBP. Compile the Add-In by choosing File | Make CodeLineCounter.dll from the menu. When the Make Project dialog box appears, be sure to specify that the executable file be placed in the same directory as Visual Basic. You want VB to have access to the Add-In later. Before you can use the Add-In, you have to make a change to the VBADDIN.INI file so that Visual Basic will know that the Add-In is available. You'll find that file in your Windows directory, and you can edit it with a text editor. Add the following line to the end of the file: CodeLineCounter.Connect=0 Save the file; then get back into Visual Basic. Open any project that you might happen to have handy. Then choose Add-In | Add-In Manager from the menu. You should see a list similar to the one shown in Figure 31.4, though the items on your screen might be different. Visual Basic's Add-In Manager. Notice that you now have an option for My AddIn. Select the Load on Startup and the Loaded/Unloaded check boxes; then click OK. As soon as the Add-In loads, it is added to VB's AddIn menu. Invoke it by choosing it from that menu, and its user interface shows onscreen. Enter the name of a project currently loaded into Visual Basic and then the name of one of its components. For example, if you have a project named Project1, which contains a form named Form1, then enter those names in the appropriate text boxes. Then click on the Count Code Lines button. You should then see the number of lines of code in the Code Lines text box. Try it with other components if you want. When you're finished, click the Done button.
Admittedly, this is a simple Add-In. It could use some work. For example, instead of having the user enter the project and component names into text boxes, you might want to use combo boxes that contain the names of all available projects and components so that the user need only select the ones he wants. But it does serve as an example of how to use the Extensibility Model in Visual Basic and should provide a starting point for further explorations into this area of programming. |
|
![]() |
![]() |
|||