Microsoft & .NETVisual BasicCreating a Visual Basic 6 Add-In

Creating a Visual Basic 6 Add-In

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

VB 6.0 contains all of the necessary tools to make your own “Add-In” to the VB-IDE. However using these tools to create your own Add-In can be quite a daunting task. There are two ways to create an Add-In, the first is to build a Class Module and add a string to the VBAddIn.Ini file. The second is to use the “Add In Designer” that comes with VB 6.0. Of the two methods, using the Add-In Designer is by far the easiest. In this tutorial I will show you how to create your own Add-In Program using the Add-In Designer Template.

Prior to creating a VB-6 Add-In, one must obviously have a project in mind. It is up to you what Add-Ins you decide to make, but for the sake of demonstrating the method, we will implement a simple number converter project that takes a number as input and converts it to the Binary, Hexadecimal and Octal equivalent. Although this program may not be of significant or practical use to many, it provides a good basis for understanding the value of using a VB IDE Add-In.

Step 1: Create a new Sub Folder in your Visual Basic Directory called ConverterAddIn. (The VB Folder is usually located under C:Program FilesMicrosoft Visual StudioVB, so your new Sub Folder would be “C:Program FilesMicrosoft Visual StudioVBConverterAddIn”).

When we selected Add-In from New Project Type we opened the Add-In Designer Template. This template is located in the “…VBTemplateProjectsAddIn.vbp” directory. If we were to make any changes to the Template’s Form or Designer and save them, the changes would be saved to the that folder and could lead to confusion at a later date. So by creating a new Folder, ConverterAddIn, renaming the Form to frmConvert and renaming the Project to ConverterAddIn we have put our project in it’s own Folder and managed to avoid any possible trouble with future add-in projects and their associated designers.

Step 2: Start Visual Basic and from the File Menu select “New Project”. From the New Project Dialog, select “AddIn” and click on the OK Button.

New Project Dialog Box
New Project Dialog Box

VB removes the existing form and replaces it with the “ADD-IN Designer Template”.

In the Project Window, click on Forms and Designers to expand them as shown below.
Also double click on frmAddIn to expose it.

If you were to Run this program in it’s current state from the VB-IDE nothing much would happen except the Intermediate Window would become visible. This is because Add-Ins created with the Add-In Template are compiled to DLLs (Dynamic Link Libraries). If you were to compile this project at this point, you would have an Add-In added to the VB-IDE called “MyAddIn”. Whenever “MyAddin” is started from the Add-Ins Tab of the Main Menu Bar a Message Box would be displayed stating “AddIn operation on”. This is a good start, but message boxes are not a great help for converting numbers.

In the Project Properties Window, accessible on the project menu, change the Project’s name to ConverterAddIn. Once you have done this, click OK, and set the Form’s name to frmConvert. Next, Save the Project to the “ConverterAddIn” Sub Folder that we created in Step 1. Use “Save Project As” from the File Menu and save the Project as “ConverterAddIn”.

The next step will be to add a Text Box, four Labels and another Command Button to frmConvert. First, change the captions of the two existing Command Buttons from “OK” and “Cancel” to “Convert” and “Exit”, then add the third Command Button, set it’s Caption to “Clear” and name it cmdClear. You can put the Labels in a Frame as shown below if you like, but it is not necessary.

Next, we will add the number input buttons as an Array. Start by placing another Command Button on the Form, set it’s Caption to 0 and it’s Name to Btn. Using the Right Mouse Button, Click on Btn to highlight it and then select “Copy”, next, Right Click the Mouse on the Form and select “Paste”. Click “yes” to VB’s question box. VB responds by placing a copy of the original Button in the upper left hand corner of the Form. Change this new Button’s Caption to 1, a check of the Properties Window reveals that this Button’s Name is Btn(1) and the original Button’s Name is Btn(0).

This is exactly what we wanted, the start of our Control Array for all of the number Buttons. Proceed with this method to establish the balance of the Array for number buttons 2 through 9. In the end you should have ten number buttons labeled zero through nine and their names will be Btn(0) through Btn(9). Your Form should now look like the following;


Form Design
Form Design

You will notice that I have changed the Form’s Caption also. Now we are ready to Code the Form.

If you look at the Form’s General Declarations Section of the Code you will see the following:

Option Explicit
Public VBInstance As VBIDE.VBE
Public Connect As Connect

These three lines of code are part of the Add-In Template. We are going to change the
Second Line to suit our Program’s purpose. Change Line 2 as follows:

'Change the name of the Designer here - on the second Line
Public Connect As NumConverter

Now add the following below “Option Explicit” in the General Declarations Section.

Dim Adecimal, digit, h, i, j, k As Integer
Dim jbin As String

Next, we will add the Subroutine DecBin to the Project, after the above Dimension Statements. We will not delve into the workings of this routine, suffice it to say that it converts an Integer or Decimal Number to a Binary Number.

Public Sub DecBin(Adecimal, jbin As String)
' Convert integer value to an equivalent string of binary digits
jbin = ""
h = Hex(Adecimal)  ' convert from integer to hexadecimal
For i = 1 To Len(h)
digit = InStr("0123456789ABCDEF", Mid(h, i, 1)) - 1
j = 8
k = 4
Do 'convert from hexadecimal to binary
jbin = jbin + Right(Str((digit  j) Mod 2), 1)
j = j - (j  2)
k = k - 1
If k = 0 Then Exit Do
Loop While j
Next i
End Sub

The following is the Code to complete the Form’s Events:

Private Sub Btn_Click(Index As Integer)
Text1 = Text1 & Index
End Sub
Private Sub CancelButton_Click()
'Place the next 3 lines here as well as in the cmdClear Sub
Text1.Text = ""
'to clear out memory when we disconnect the Add-In
Text1.SetFocus
Frame1.Visible = False
Connect.Hide
End Sub
Private Sub cmdClear_Click()
Text1.Text = ""  'Clears out TextBox and memory
Text1.SetFocus   'to allow a new conversion
Frame1.Visible = False
End Sub
Private Sub Form_Load()
'Center Form on the Screen
Me.Move (Screen.Width - Me.Width) / 2, (Screen.Height - Me.Height) / 2
End Sub
VB-6 Add-In Tutorial
Page 6
Private Sub OKButton_Click()
'Comment out original Code placed here by the Add-In Template
 'MsgBox "AddIn operation on: " & VBInstance.FullName
Adecimal = Val(Text1) 'get the number from the TextBox
Frame1.Visible = True
'Make the Call to convert from decimal to binary
Call DecBin(Adecimal, jbin)
Label1.Caption = "Decimal = " & Text1
Label2.Caption = "Binary = " & jbin  ' display the result
Label3.Caption = "Hex   = " & Hex(Adecimal)
Label4.Caption = "Octal  = " & Oct (Adecimal)
End Sub

That’s it for the Coding of the Projects Events, now on with the next part.

Clicking on “Designers and then on Connect(Connect.Dsr)” in the Project Window will Expose the Designer itself.


In the Properties Window, Click on Name and change it to “NumConverter”, then Save it by right clicking on it in the Project Window as use “Save Connect.Dsr As” and Save it as “NumConverter” in our ConverterAddIn SubFolder. Your Project and Properties Windows should look as shown below;


The next step is to open the Designer by Right Clicking with the mouse on “NumConverter(NumConverter.Dsr)” under the Designers Folder in the Project Window. This opens up the secondary Menu and we want to select “View Object”.

Now expand the Designer by Clicking on it’s “Maximize Button” in the upper right hand corner of the Designer’s Form to reveal all of it’s Input Boxes;


Blow Up!

Change the “Add-In Display Name” from “MyAddIn to: “Number Converter”

Change the “Add-In Description” from “Add-In Project Template” to: “Convert a Base Ten Number to Binary, Hex and Octal”.

The “Application” should read “Visual Basic” and the “Application Version” should be “Visual Basic 6.0” as shown above. The Initial Load Behavior should be set to Startup and the “Addin is command-line safe” box should be left Unchecked. Close the Designer Form.

Now open the Designer’s Code Window by Right Clicking on “NumConverter” under Designers in the Project Window to expose it’s Menu and select “View Code”;

In the General – Declarations Section you will see:

Option Explicit
Public FormDisplayed     As Boolean
Public VBInstance       As VBIDE.VBE
Dim mcbMenuCommandBar     As Office.CommandBarControl
Dim mfrmAddIn         As New frmAddIn
Public WithEvents MenuHandler As CommandBarEvents
' ***Change the Dim mfrmAddIn As New frmAddin to the following;***
Dim mfrmConvert As New frmConvert

Next, in the General Section Pull Down Box select the Down Arrow


MsgBox Generator

and expose the “AddinInstance – OnConnection” Sub


Blow Up!

Remark out the “Debug.Print VBInstance.FullName line as shown. Then scroll down five lines to the

Set mcbMenuCommandBar = AddToAddInCommandBar ("My AddIn")

Change this line to read:

Set mcbMenuCommandBar = AddToAddInCommandBar ("ConverterAddIn")

Ok, we are just about done. By making the above changes we have forced VB to use an Add-In
Name and Form that reflects our Number Converter as opposed to the name “MyAdd-In”.

There is just a bit of house cleaning that remains however, and we will make the final Designer
Code changes now. Recall previously that we made the following change to the Designers General Declarations Section:

***Change the Dim mfrmAddIn As New frmAddin to the following;***
Dim mfrmConvert As New frmConvert

Well unfortunately the term “mfrmAddIn” is still embedded in our Designer’s code eight
more times, and “frmAddIn” remains in one location. These occur in the following Sub
Routines in addtion to the above Dim Statement:

Term Change To: In Subroutine Occurrences
mfrmAddIn mfrmConvert General-Show 5
mfrmAddIn mfrmConvert General-Hide 1
mfrmAddIn mfrmConvert AddinInstance-OnDisconnection 2
frmAddIn frmConvert General-Show 1

You can make the necessary corrections using VB’s “Find and Replace” (Ctrl H) procedure under the Edit Tab of the Main Menu. (Don’t worry if you miss making all of the above changes correctly, because when you try to compile the DLL, VB will let you know if there is a mismatch and then you can make the proper correction).

Ok last step, Save all of your work then click on the File Tab of the Main Menu.

Select “Make ConverterAddIn.dll..” when VB finishes making the dll, click on “Add-Ins” on the Main Menu Bar and select “Add-In Manager” to see “Number Converter” listed under the “Available Add-Ins”, Highlight it and click on Loaded/Unloaded in the lower right corner, click cancel to close. Now click on “Add-Ins” (Main Menu Bar) again and “Number Converter” appears on the drop down list, select it and the Add-In opens ready to use. From here on out the “Number Converter Add-In” will be available from the Main Menu Add-Ins tab every time that VB is started.

Well, there we have it, our very own Add-In! It may be just about as useful as a chocolate tea pot to most people. However, as ever with VB, the things that you can make are limited only by your imagination. If you have made a wonderful Add-In that you think that everyone will benefit from, email it to us and it may well end on the demos page!

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories