Microsoft & .NETVisual BasicMaking Your First Addin

Making Your First Addin content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Since VB 5 Add-Ins have become more popular. Many companies
such as Sheridan Software Systems have made add ins. The best thing about them is that
they integrate into the VB IDE very easily, and open up the possibility of using the VBIDE
Object Model. When you use the Object Browser to view the VB5Extensibility Type Library,
there are loads of methods and properties for you to use. This can be confusing.

VB 5 provides a template Add-In but does not document it
very well. In this How-To we show you how to build a small Add-In and an installer program
for it.

NOTE: You need to make a reference to the following files
(Do this by going Project, References)

  • Microsoft Office 8.0 Type Library (MSO97.DLL)
  • Microsoft Visual Basic 5 Extensibility (VB5Ext.olb)

Make a new Standard EXE project. Add a mdoule (AddIn). Set
the projects Startup Object to Submain and copy the following code into the module:

Option Explicit

Declare Function GetPrivateProfileString Lib "kernel32" _
  Alias "GetPrivateProfileStringA" _
  (ByVal AppName As String, ByVal KeyName As String, _
  ByVal keydefault As String, _
  ByVal result As String, ByVal resultSize As Long, _
  ByVal filename As String) As Long
Declare Function WritePrivateProfileString& _
  Lib "kernel32" Alias _
  "WritePrivateProfileStringA" (ByVal AppName$, _
  ByVal KeyName$, ByVal keydefault$, ByVal filename$) _
  As Long

Const AddInName = "VBSAddIn"

Sub InstallAddIn()
' Add a reference in the VBADDIN.INI file
Dim result As String, errCode As Long

' try to read the entry in VBADDIN.INI
result = Space$(256)
GetPrivateProfileString "Add-Ins32", AddInName _
  & ".Connect", "***", result, _
  Len(result), "vbaddin.ini" 

If Left$(result, 3) = "***" Then
  ' the entry is not there, so we must write it
  errCode = WritePrivateProfileString("Add-Ins32", _
    AddInName & ".Connect", "0", _
End If

' If errCode Then
'  MsgBox "Error while accessing VBADDIN.INI", vbCritical
' End If

End Sub

Sub Main()
If App.StartMode = 0 Then
End If
End Sub

Make a new ActiveX DLL project. Rename the class
Connect.cls. Rename the project VBSAddin. Copy the following code into the class module:

Option Explicit

Implements IDTExtensibility

'the instance of the VB IDE Public VBInstance
'command bar event handler
Public WithEvents MenuHandler As CommandBarEvents

'used to remember if main form is currently visible
Public FormIsVisible As Boolean

'the main form of the add-in 
Private frmMain As frmAddIn

'the connection to the item in the AddIn menu
Private mcbMenuCommandBar As Office.CommandBarControl

Sub ShowForm()
' show the main addin form
On Error Resume Next

If frmMain Is Nothing Then
  Set frmMain = New frmAddIn
End If

Set frmMain.VBInstance = VBInstance
Set frmMain.Connect = Me
FormIsVisible = True
End Sub

Sub HideForm()
' hide the main addin form
On Error Resume Next

FormIsVisible = False
End Sub

Sub UnloadForm()
' unload the main addin form
On Error Resume Next

FormIsVisible = False
Unload frmMain
End Sub

Private Sub IDTExtensibility_OnConnection(ByVal VBInst As Object, _
  ByVal ConnectMode As vbext_ConnectMode, _
  ByVal AddInInst As VBIDE.AddIn, custom() As Variant)
' this method is invoked when the addin is added to VB
On Error GoTo OnConnection_Err

'save the vb instance
Set VBInstance = VBInst

If ConnectMode = vbext_cm_External Then
 'this addin is being started by the wizard toolbar
  'connect this addin to an item in the AddIn menu
  Set mcbMenuCommandBar = AddToAddInCommandBar(AddInCaption)
  'sink the event
  Set Me.MenuHandler = VBInst.Events.CommandBarEvents(mcbMenuCommandBar)

  If ConnectMode = vbext_cm_AfterStartup Then
    'if this addin is being added to the addin menu
    'see if we should display its form immediately
    If GetSetting(AddInName, "Settings", "DisplayOnConnect", "0") = "1" Then
    End If
  End If
End If

Exit Sub

 Dim msg As String
 msg = "Unable to start add-in " & AddInName & _
   vbCr & vbCr & Err.Description
 MsgBox msg, vbCritical

End Sub

Private Sub IDTExtensibility_OnDisconnection _
  (ByVal RemoveMode As vbext_DisconnectMode, custom() As Variant)
' this method is invoked when the addin is removed
On Error Resume Next

'delete the command bar entry

'remember current state of visibility
If FormIsVisible Then
  SaveSetting AddInName, "Settings", _
    "DisplayOnConnect", "1"
  FormIsVisible = False
  SaveSetting AddInName, "Settings", _
    "DisplayOnConnect", "0"
End If

'unload the form
Unload frmMain
Set frmMain = Nothing

End Sub

Private Sub IDTExtensibility_OnStartupComplete(custom() As Variant)
' this method is invoked when the addin has
' completed its loading

'show the addin form, according to saved visibility state
If GetSetting(AddInName, "Settings", _
  "DisplayOnConnect", "0") = "1" Then
End If
End Sub

Private Sub IDTExtensibility_OnAddInsUpdate(custom() As Variant)
' this method is invoked when the addin menu is updated
End Sub

Private Sub MenuHandler_Click(ByVal CommandBarControl As Object, _
  handled As Boolean, CancelDefault As Boolean)

' this event is invoked when the menu item is clicked
End Sub

Function AddToAddInCommandBar(sCaption As String) _
  As Office.CommandBarControl

' create an item in the addin menu
' and connect it to this addin
Dim cbMenuCommandBar As Office.CommandBarControl
Dim cbCommandBar As Office.CommandBarControls
Dim cbMenu As Object

'exit if any error
On Error GoTo AddToAddInCommandBar_Exit

'see if we can find the Add-Ins menu - exit if fails
Set cbMenu = VBInstance.CommandBars("Add-Ins")
If cbMenu Is Nothing Then Exit Function

'add an item to the command bar
Set cbMenuCommandBar = cbMenu.Controls.Add(1)

'set its caption
cbMenuCommandBar.Caption = "VB Square Add-In"

'return a reference to the menu item
Set AddToAddInCommandBar = cbMenuCommandBar


End Function

Add a form (frmAddIn) and copy the following code into the
General Declarations procedure:

Public VBInstance As VBIDE.VBE
Public Connect As Connect

Option Explicit

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories