Create a new ActiveX DLL project. Add a new class module and paste this source code into it. You should name this class Connect. If you have any questions, email us at help@codeoftheweek.com
'----------------------------------------------------------------------
'
' Module Name: Connect - Interface for VB5/6 AddIn
' Written By: C&D Programming Corp.
' Create Date: 3/99
' Copyright: Copyright 1999 by C&D Programming Corp. Source
' code may not be reproduced except for use in a
' compiled executable. All rights reserved. If
' you would like to reprint any or all of this
' code please email us at info@codeoftheweek.com
'
' Notes:
' In order for your add-in to have a description when you select
' Add-Ins / Add-In Manager... you will need to update the
' Description property for this class in the Object Browser.
' You can do this by launching the Object Browser with the F2
' key and selecting your project name from the top combo box
' (in this case CloseAllWindows). On the bottom half on the left
' side you will see a list of modules with Connect included in
' that list. Right-click on Connect and select Properties.
' Update the Description field with the description you want to
' appear in the Add-In Manager listing. This description can
' contain spaces.
'----------------------------------------------------------------------
Option Explicit
Implements IDTExtensibility ' for add-ins we need to inherit this interface
' This object is used in order to gather information about the VB
' interface. It provides access to nearly everything about the IDE.
' You probably want to check out all the properties and methods in
' the object browser and help files.
Public VBInstance As VBIDE.VBE
' This is used to track the item we add to the menu
Dim mcbMenuCommandBar As Office.CommandBarControl
' This is user to trap the event raised when the menu item is selected.
' Take note of the WithEvents option so that we get the raised events.
Public WithEvents MenuHandler As CommandBarEvents
'----------------------------------------------------------------
' This subroutine does the actual work of closing all the
' open code and designer windows.
'----------------------------------------------------------------
Private Sub CloseAllWindows()
Dim win As Window
' let's close all code windows and designer windows.
For Each win In VBInstance.Windows
If win.Type = vbext_wt_CodeWindow Or _
win.Type = vbext_wt_Designer Then
win.Close
End If
Next
End Sub
'----------------------------------------------------------------
' This event fires when the menu is clicked in the IDE
'----------------------------------------------------------------
Private Sub MenuHandler_Click(ByVal CommandBarControl As Object, _
handled As Boolean, CancelDefault As Boolean)
CloseAllWindows
End Sub
'----------------------------------------------------------------
' This routine will add the item to the Add-ins menu.
'----------------------------------------------------------------
Function AddToAddInCommandBar(sCaption As String) As Office.CommandBarControl
Dim cbMenuCommandBar As Office.CommandBarControl 'command bar object
Dim cbMenu As CommandBar
On Error GoTo AddToAddInCommandBarErr
' see if we can find the Add-Ins menu
Set cbMenu = VBInstance.CommandBars("Add-Ins")
If cbMenu Is Nothing Then
' not available so we fail
Exit Function
End If
' Add it to the command bar
Set cbMenuCommandBar = cbMenu.Controls.Add(msoControlButton)
' Set the caption
cbMenuCommandBar.Caption = sCaption
Set AddToAddInCommandBar = cbMenuCommandBar
Exit Function
AddToAddInCommandBarErr:
Err.Raise Err.Number, "AddToAddInCommandBar", Err.Description
End Function
'----------------------------------------------------------------
' The routines after this comment are required to completely
' implement the IDTExtensibility interface.
'----------------------------------------------------------------
'----------------------------------------------------------------
' Occurs when an add-in is connected to the Visual Basic IDE,
' either through the Add-In Manager dialog box or another
' add-in.
'----------------------------------------------------------------
Private Sub IDTExtensibility_OnConnection(ByVal VBInst As Object, _
ByVal ConnectMode As vbext_ConnectMode, ByVal AddInInst As VBIDE.AddIn, _
custom() As Variant)
On Error GoTo error_handler
' save the vb instance for later usage
Set VBInstance = VBInst
If ConnectMode = vbext_cm_External Then
' In this case the add-in does not make sense to call externally. We
' could use this method to show a form or other interface when necessary
Else
' add an item to the menu listing under the Add-Ins menu.
Set mcbMenuCommandBar = AddToAddInCommandBar("Close All Windows")
' sink the event so we can trap the menu selection when you click on it.
Set Me.MenuHandler = VBInst.Events.CommandBarEvents(mcbMenuCommandBar)
End If
' Let's perform the close all windows action even on startup.
If ConnectMode = vbext_cm_AfterStartup Then
CloseAllWindows
End If
Exit Sub
error_handler:
MsgBox "An error occurred in OnConnection while trying to startup this add-in. The message is: " & Err.Description
End Sub
'----------------------------------------------------------------
' Occurs when an add-in is disconnected from the
' Visual Basic IDE, either programmatically or
' through the Add-In Manager dialog box
'----------------------------------------------------------------
Private Sub IDTExtensibility_OnDisconnection(ByVal RemoveMode As vbext_DisconnectMode, custom() As Variant)
On Error Resume Next
'delete the command bar entry
mcbMenuCommandBar.Delete
End Sub
'----------------------------------------------------------------
' Occurs automatically when changes to the Vbaddin.Ini file
' are saved.
'----------------------------------------------------------------
Private Sub IDTExtensibility_OnAddInsUpdate(custom() As Variant)
' for this project there is nothing we need to do here
End Sub
'----------------------------------------------------------------
' Occurs when the startup of the Visual Basic IDE is complete.
'----------------------------------------------------------------
Private Sub IDTExtensibility_OnStartupComplete(custom() As Variant)
' for this project there is nothing we need to do here
End Sub