Create Your Own VB Add-Ins

Why should you createAdd-Ins? We programmers always feel that we are short of several features whileworking with Microsoft tools, it seems that Microsoft hasn’t yet developed thetool we needed. In fact, Microsoft has done a wonderful job of adding newfeatures to each release of its development tools. Obviously, Microsoft can’tdesign features to fulfill the needs of each and every programmer around theworld so Microsoft made Visual Basic and extensible product, thereby providingthe way for VB developers to create their own features in VB.

What is EOM?

EOM Stands for Extensibility Object Model. You might ask whatis extensibility? Extensibility is the capability to extend, stretch, thefunctionality of different development tools, specifically, Microsoft Integrateddevelopment environment (IDE). The IDE provides you with a programming interfaceknown as the extensibility object model, a set of powerful interfaces forcustomizing the environment. It allows you to hook into the IDE to createextensions known as Add-Ins. A good system is the one which can be extendedwithout jeopardizing the primary functionality of the system.

To implement extensibility features, VB offers the powerfulExtensibility Object Model. Through EOM, many core objects in VB itself areavailable to you at no extra charge. EOM is not that easy to learn, this articlewill provide you only the basics of Add-in creation, you will have to delve intothis vast field yourself to explore the wonders you can do using the EOM.

EOM consists of six loosely couple packages of objects withmethods that implement key services of the VB development model. These are:

  • Core Objects

  • Form Manipulation

  • Event Response

  • Add-In Management

  • Project and Component Manipulation

  • Code Manipulation

Core Objects

This package is the main package used in the creation ofAdd-Ins. It has the following objects:

  • The root object

  • The IDTExtensibility Interface object

  • The Visual Basic instance variable

The Root Object

VBE is the root object in Visual Basic. The VBE object is the base object for every extensibility object and collection in Visual Basic. Each object and collection owns a reference to the VBE property. The collections owned by the VBE object include the following:

  • VBProjects

  • Windows

  • CodePanes

  • CommandBars

The VBProjects Collection

This collection enables you to access a set of VB properties.This feature can be helpful if your development environment has an establishedprocess for developing software. Some of the key properties and methods of thiscollection are:

Filename: returns the full pathname of the group project file

Startproject: returns or sets the project that will startwhen users choose start menu from the run menu, click the run button, or pressF5 key.

AddFromFile: This is a method that enables the users to addor open a project or group object. Its only required argument is the stringrepresenting the path of the file you want to add.

AddFromTemplate: This method enables you to add projecttemplates into the VBProjects collection. Its only required argument is thestring representing the path of the file you want to use as a template.

The Windows Collection

With the windows collection, you can access the windows suchas the project and properties windows. This collection enables you to access agroup of all currently open code windows and designer windows.

TheIDTExtensibility Interface Object

The IDTExtensibility Interface Object exposes the publicmethods and properties of the extensibility model. By exposes, I mean thatbecause you don’t directly use the services, methods and properties of theunderlying extensibility model, you need to invoke the methods of the model’sagent, so to speak. You can think of interfaces as public agents for the privateimplementation of an extensibility model object you instantiate.

The Visual BasicInstance Variable

This is also known as dynamic identification variable.It identifies a particular instance of your VB session. This instance identifierenables you to have separately identifiable running instances of VB in memory.

The instance variable is of the type VBIDE.VBE. To use thisvariable, declare it in a class module or general module.

Please refer to Microsoft website (http://www.microsoft.com)for complete details of these packages. Please understand that I can not explaineach and every detail of these packages in this short article. Lets get startedwith the creation of the Add-In.

We will build a simple Add-In that will count the number oflines of code for a given program component.

To begin creating the Add-In, start a new project. Choose theAddIn project type. The AddIn project type includes many components necessaryfor creating VB Add-Ins. There is a form that you can modify to provide a userinterface for your Add-In. There is also a designer module that contains thefour methods that are needed for the Add-In’s interface to VB.

Remove the Ok and Cancel buttons from the form and add thefollowing controls to the form:

Control Type

Property

Value

Form

Name

FrmAddIn

 

Caption

Code Line Counter

Label

Name

LblProject

 

Caption

Project

TextBox

Name

TxtProject

Label

Name

LblComponent

 

Caption

Component

TextBox

Name

TxtComponent

CommandButton

Name

CmdCountCodeLines

 

Caption

Count Code Lines

CommandButton

Name

CmdDone

 

Caption

Done

Label

Name

LblCodeLines

 

Caption

Code Lines

TextBox

Name

TxtCodeLines

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
Connect.Hide
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 theproject and component names that were typed into the form’s textbox controlsto assign that component to the objVBComponent object. Note the hierarchy usedto 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 itsVBProjects collection, that project’s VBComponents collection is accessed byusing the strVBComponent string as a key argument for the collection’s itemmethod. This long sequence of referencing ultimately assigns the specifiedcomponent that is part of the specified project (strVBProject) to theobjVBComponent object.

The following line of code

txtCodeLines.Text = Str(objVBComponent.CodeModule.CountOfLines)

is used to access the CodeModule of the newly assignedobjVBComponent object. The countoflines property of the CodeModule objectcontains the number of lines of code in that particular component. This numberis assigned to the txtCodeLines textbox so that the user can see the results.

The second event procedure that was added is thecmdDone_click() event. This contains only a single line of code that calls theConnect object’s Hide method, hiding the Add-In’s user interface. TheConnect object is an instance of the Connect class, which, as you mightremember, is a part of the AddIn project. It is defined in the form’s Generaldeclarations section.

In Connect Designer, in the AddInInstance_OnConnectionprocedure, 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 boxappears, be sure to specify that the executable file be placed in the samedirectory 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 tothe VBADDIN.INI file so that VB will know that the AddIn is available. You willfind this file in the Window’s directory. Add the following line in the end ofthe file:

CodeLineCounter.Connect=0

Save the file; then get back into VB. Open any project thatyou might happen to have handy. Then choose Add-In | Add-In Manager from themenu. You should see a list of Add-Ins. Select the codelinecounter, select theLoad on startup and the Loaded/Unloaded check boxes, then click OK.

Invoke the Add-In by choosing it from that menu, and its userinterface shows onscreen. Enter the name of the project currently open and thenthe name of the component. For example, the open project’s name isproject1.vbp, enter project1 in the project textbox and the form’s name isform1.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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may