Library tutorials & articles

Create your own Add-Ins in VB

Examining the sample code

Let's see whats in the code, the 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 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, 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 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.

In Connect Designer, 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'

Save the project and compile the AddIn 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 VB. 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 VB will know that the AddIn is available. You will find this file in the Window's directory. Add the following line in the end of the file:

CodeLineCounter.Connect=0

Save the file; then get back into VB. 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 of Add-Ins. Select the codelinecounter, select the Load on startup and the Loaded/Unloaded check boxes, then click OK.

Invoke the Add-In by choosing it from that menu, and its user interface shows onscreen. Enter the name of the project currently open and then the name of the component. For example, the open project's name is project1.vbp, enter project1 in the project textbox and the form's name is form1.frm, enter form1 in the component textbox and click "Count Code Lines" button and you will see the number of lines in the "Code Lines" textbox.

Comments

  1. 09 Sep 2009 at 06:07

    Hello i am using VB6.0 at the moment, but i can not design a program to use printer. please, help me. thanks.

  2. 06 Dec 2005 at 05:15
    Mr. S.S. Ahmed's Create your own Add-Ins in VB is a perect guide to create Add-Ins.
    It helped to create my Add-In in just 5 minutes. An Excellent guide.


  3. 01 Jan 1999 at 00:00

    This thread is for discussions of Create your own Add-Ins in VB.

Leave a comment

Sign in or Join us (it's free).

S.S. Ahmed S.S. Ahmed is a senior IT Professional and works for a web and software development firm. Ahmed is a Microsoft Office SharePoint Server MVP. Ahmed specializes in creating database driven dynamic we...

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

We'd love to hear what you think! Submit ideas or give us feedback