|
Create your own VB Add-InsDownload Source Code (63.4 kb)
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 hasn’t yet developed the tool we needed. 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 around the world so Microsoft made Visual Basic and extensible product, thereby providing the way for VB developers to create their own features in VB. What is EOM?EOM Stands for Extensibility Object Model. You might ask what is extensibility? Extensibility is the capability to extend, stretch, the functionality of different development tools, specifically, Microsoft Integrated development environment (IDE). The IDE provides you with 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 which can be extended without jeopardizing the primary functionality of the system. To implement extensibility features, VB offers the powerful Extensibility Object Model. Through EOM, many core objects in VB itself are available to you at no extra charge. EOM is not that easy to learn, 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 couple packages of objects with methods that implement key services of the VB development model. These are: · Core Objects Core ObjectsThis package is the main package used in the creation of Add-Ins. It has the following objects: · The root object The Root ObjectVBE 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 The VBProjects CollectionThis 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 The Windows CollectionWith the windows collection, you can access the 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 ObjectThe 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, so to speak. You can think of interfaces as public agents for the private implementation of an extensibility model object you instantiate. The Visual Basic Instance VariableThis is also known as 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. Please refer to Microsoft website (http://www.microsoft.com) for complete details of these packages. Please understand that I can not explain each and every detail of these packages in this short article. Lets get started with the creation of the Add-In.
………………………………………………. 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 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. 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. Set mcbMenuCommandBar = AddToAddInCommandBar(“My AddIn”) Change the “My AddIn” to “Code Line Counter”. 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. Download Source Code (63.4 kb)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||