asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search





Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

Create Your Own Visual Basic Add-Ins
By S.S. Ahmed
Rating: 3.1 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

    Introduction


    Programmers like me create add-ins primarily because we feel short of features when working with Microsoft tools.I It seems at times that Microsoft hasn't yet developed the tool we need. In fact, Microsoft has done a wonderful job of adding new features to each release of its development tools. Obviously, Microsoft can't design features to fulfill the needs of each and every programmer so it made Visual Basic (VB) an extensible product, thereby providing the way for VB developers to create their own features.

    Download source code

    EOM Defined

    EOM stands for Extensibility Object Model. You might ask what is extensibility? Extensibility is the capability to extend or stretch the functionality of different development tools, specifically the Microsoft Integrated Development Environment (IDE). IDE provides a programming interface known as the Extensibility Object Model, a set of powerful interfaces for customizing the environment. It allows you to hook into the IDE to create extensions known as add-ins. A good system is the one that can be extended without jeopardizing the primary functionality of the system.

    To implement extensibility features, VB offers the powerful EOM. Through EOM, many core objects in VB itself are available to you at no extra charge. EOM is not that easy to learn, and this article will provide you only the basics of add-in creation. You will have to delve into this vast field yourself to explore the wonders you can do using the EOM.

    EOM consists of six loosely coupled packages of objects with methods 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 of add-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 established process for developing software. Some of the key properties and methods of this collection are:

    • Filename: Returns the full pathname of the group project file.
    • Startproject: Returns or sets the project that will start when users choose the Start menu from the Run menu, click the Run button, or press the F5 key.
    • AddFromFile: This is a method that enables the users to add or open a project or group object. Its only required argument is the string representing the path of the file you want to add.
    • AddFromTemplate: This method enables you to add project templates into the VBProjects collection. Its only required argument is the string representing the path of the file you want to use as a template.

    THE WINDOWS COLLECTION

    With the Windows collection, you can access windows, such as the project and properties windows. This collection enables you to access a group of all currently open code windows and designer windows.

    The IDTExtensibility Interface Object

    The IDTExtensibility Interface object exposes the public methods and properties of the extensibility model. By exposes, I mean that because you don't directly use the services, methods, and properties of the underlying extensibility model, you need to invoke the methods of the model's agent. You can think of interfaces as public agents for the private implementation of an extensibility model object you instantiate.

    The Visual Basic Instance Variable

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

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

    Please refer to the Microsoft Web site (see http://www.microsoft.com) for complete details of these packages. Please understand that I cannot explain each and every detail of these packages in this short article.

    Creating the Add-In

    We will build a simple add-in that will count the number of lines of code for a given program component. To begin creating the add-in, start a new project. Choose the AddIn project type. The AddIn project type includes many components necessary for creating VB add-ins. There is a form that you can modify to provide a user interface for your add-in. There is also a designer module that contains the four methods that are needed for the add-in's interface to VB. Users may read more about this at the Microsoft site (see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon98/html/vbconcreatingaddin.asp).

    Remove the OK and Cancel buttons from the form, and add the following 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

    The Code

    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 text box
    txtCodeLines.Text = Str(objVBComponent.CodeModule.CountOfLines)
    
    
    
    End Sub
    
    Private Sub cmdDone_Click()
    
    'Hide the AddIn window
    Connect.Hide
    
    End Sub
    
    
    .......................................................

    Let's see what's 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 text box 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. The current 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 text box 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 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 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 add-in 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, and then click OK.

    Invoke the add-in by choosing it from that menu, and its user interface shows on screen. 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 text box and the form's name is form1.frm. Enter form1 in the component text box and click the Count Code Lines button, and you will see the number of lines in the Code Lines text box.

    About the Author

    S.S. Ahmed is a senior software engineer in a software development company that specializes in Web application development. He can be reached with questions or comments at ss_ahmed1@hotmail.com.

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Other Articles
    Apr 27, 2004 - Develop and Customize Web Parts with Custom Tool Parts
    Tool Parts provide an interface for Web Part properties well beyond the capabilities of the default property pane. In this article Gayan Peiris shows how to customize Web Parts with custom Tool Parts.
    [Read This Article]  [Top]
    Apr 7, 2004 - Reusable Components in ASP.NET 2.0, Object Binding and Precompilation
    This article demonstrates how to create a reusable component in ASP.NET 2.0 and then consume it from an ASP.NET page. Also learn how the ObjectDataSource control can be used to directly bind the output of an object to the controls in an ASP.NET page and how precompilation can be used to increase the performance of the Web application and catch compilation errors.
    [Read This Article]  [Top]
    Mar 31, 2004 - Build a Managed BHO and Plug into the Browser
    Browser Helper Objects (BHOs) are COM components that communicate with Internet Explorer to enrich the browsing experience. Michele Leroux Bustamante returns to the world of COM to show you how to build a managed BHO with the help of the .NET Framework's COM interoperability features.
    [Read This Article]  [Top]
    Feb 18, 2004 - Customizing SharePoint Web Parts with Custom Properties
    In addition to creating custom Web Parts for SharePoint Portal Server, developers can actually create their own custom properties to further enhance Web Part appearance and behavior. Gayan Peiris explains the process and provides all the necessary code.
    [Read This Article]  [Top]
    Sep 26, 2003 - Accessing Shared Resources Using ASP.NET
    Accessing shared resources is a challenge for many ASP.NET developers. Tony Arslan explains how a simple serviced component can solve this infamous problem.
    [Read This Article]  [Top]
    Oct 2, 2002 - Function Pointers and COM
    Using callbacks and function pointers in VB can be risky and complicated. Ben Garcia explains his work-around for the function pointer issue he encountered while creating the VB version of his SNMP component.
    [Read This Article]  [Top]
    Sep 4, 2002 - Creating an SNMP Component - Part 2
    In part two of this intriguing article series, Ben Garcia shows how to build an updated and improved SNMP component in VC++ AND VB, and he briefly explains why limitations in VB make VC++ a better language for developing this type of application.
    [Read This Article]  [Top]
    Jul 23, 2002 - Creating an SNMP Component
    Ben Garcia sheds some light on the Simple Network Management Protocol (SNMP). First he provides a history of SNMP, then he dives right into its architecture. Finally, he shows how to build a COM component that communicates with SNMP-enabled devices.
    [Read This Article]  [Top]
    Jun 26, 2002 - Accessing Caller ID from the Web - Part 1
    Paul Apostolos begins his series on using Web services and the MSComm32.OCX component to access caller id information from a Web page. In part 1, learn how to write the Visual Basic program that runs on the server and updates a database with incoming callers.
    [Read This Article]  [Top]
    Nov 20, 2001 - Creating a Server Component with VB - Redesigned - Part 2
    Doug Dean explains different methods of retrieving and manipulating data from a database in a VB DLL so that it is ready to be rendered in a browser.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry