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