July 18, 2018
Hot Topics:

Making Your First Addin

  • November 19, 2002
  • By Sam Huggill
  • Send Email »
  • More Articles »

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

Page 3 of 4

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.


Thanks for your registration, follow us on our social networks to keep up-to-date