Building a Visual Basic Addin

Being able to build your own add-ins is one of the more powerful features of Visual Basic. To make it work, you will need to do a few things. The first thing you must do is add your application into the .ini file that contains a list of Visual Basic add-ins. You may find it hard to imagine that we are still using an .ini file, but we are. If you do not remember what an .ini file is, it is simply a text file that contains information for a particular program. For the most part, it has been replaced by the registry.

The most critical piece of information that we put into the add-in .ini file is the identifier for the add-in. We will be using the standard project name dot class name. Let us begin by creating a function to add the information into the .ini file. Create a new Visual Basic ActiveX DLL project and call it CodeGenerator. Call the default class CodeGeneratorClass. Add a .bas module to the project and call it basMain. We are going to use an API function called WritePrivateProfileString to write to the .ini file. If you are not familiar with API functions, they are functions that are provided by Windows. We can use these functions in our Visual Basic application by declaring the function in our Visual Basic application in a . bas module using the Declare keyword. Add the following code to basMain to declare the windows function WritePrivateProfileString in your Visual Basic project:

Public Declare Function WritePrivateProfileString _

Lib "kernel32" _

Alias "WritePrivateProfileStringA" _ (ByVal ApplicationName As String, _ ByVal KeyName As Any, _ ByVal KeyDefault As Any, _ ByVal FileName As String) As Long

If you are wondering what all of this means, kernel32 is the DLL where the function resides. The alias, WritePrivateProfileStringA, is the real name of the function (there are usually two versions, one for Unicode strings and one for regular strings). WritePrivateProfileString is the name we give it and which we will use in our code. ApplicationName is the name of the section that you want to add this information to KeyName is the information you want to put into the ApplicationName section KeyDefault is the default value of ApplicationName, and FileName is the name of the .ini file you want to put the information in. In our case, ApplicationName is Add-Ins32, the KeyName is CodeGenerator .CodeGeneratorClass, the KeyDefault is 0 and the name of the file is VBADDIN.INI. You can probably find VBADDIN. INI in your Windows directory. My VBADDIN.INI has the following entries:

[Add-Ins32]

MTxAddIn2.RegRefresh=3

DTCFrameWork.AddIn=0

RVBAddInMenus.Connector=0

RVBAddIn.Connector=0

vbscc=3

VBObjTest.Application.1=0 VisioUMLSolution.VisioUMLSolution=3

Of course, we could manually enter the name of our component into the .ini file, but it is safer to use the Windows API function.

Fortunately, Visual Basic now comes with a tool that allows us to get the proper declarations for all our Windows API functions. If you go to the Windows Start/Programs/Microsoft Visual Studio 6/Microsoft Visual Studio 6 Tools/API Text Viewer you will pull up the API text viewer. If it is the first time you are using it, you will have to load the information first. To do this, in the API Text Viewer Select File/Load Text File:

Select Win32API .txt. If you select Declares from the dropdown list, you will find our API function.

I have changed the names of the parameter in the function declaration. You can do this as long as you have the right number of parameters and each parameter has the correct data type.

API functions can extend the Visual Basic language. For the most part these functions have been wrapped by functions within Visual Basic. Sometimes, though, using an API function is the only way to get the information you require or to do what you need to do.

Next we will add a function that uses our Windows API, called AddToIni, to basMain:

Public Sub AddToINI()

Dim lngReturn As Long lngReturn = WritePrivateProfileString("Add-Ins32", _ "CodeGenerator.CodeGeneratorClass", "0", "VBADDIN.INI") MsgBox "Add-in is now entered in VBADDIN.INI file." End Sub

We will show an easy way to use this sub later. The next step is to get access to the Visual Basic IDE object hierarchy so that we can manipulate objects in the Visual Basic IDE. To do this, we will need access to the extensibility object.

Was this article helpful?

0 0
365 Days Of Motivation

365 Days Of Motivation

Stop Wasting Time And Learn How To Stay Motivated. Finally! Discover How To Stop Your Mind From Wandering, And Upgrade Your Motivation. You Can Hack Your Motivation Levels, Allowing You To Take Your Life To The Next Level.

Get My Free Ebook


Post a comment