Browse DevX
Sign up for e-mail newsletters from DevX


Create Your Own VB Add-Ins : Page 2

Why should you create Add-Ins? We programmers always feel that we are short of several features while working with Microsoft tools, it seems that Microsoft hasnt yet developed the tool we needed. This article shows you how you can use the IDE's Extensibility Object Model to design your own add-in.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Here is the code:

Public VBInstance As VBIDE.VBE

Public Connect As Connect

Option Explicit

Private Sub cmdCountCodeLines_Click()

Dim strVBProject As String

Dim strVBComponent As String

Dim objVBComponent As VBComponent

'Forms 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 addin window


End Sub

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:


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.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date