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.

You might also like...

Comments

About the author

S.S. Ahmed United States

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...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“A computer lets you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila” - Mitch Ratcliffe