Visual Basic Code of the Week (COTW)
http://www.codeoftheweek.com
Issue #96
Online Version at http://www.codeoftheweek.com/membersonly/bi/0096.html (paid subscribers only)
All content and source code is Copyright (c) 1999 by C&D Programming Corp. No part of this issue can be reprinted or distributed in any manner without express written permission of C&D Programming Corp. Word, Excel and Visual Basic are trademarks of Microsoft Corp.

Cool Software

Download a free copy of NetMon - Your Internet Performance Monitor at http://www.codeoftheweek.com/netmon/index.html

New Visual Basic Site

We are looking for some qualified individuals to work on a new Visual Basic site. The first task is developing a high-quality resource guide for Visual Basic developers. This resource guide will be a well-organized list of web sites that have very good resources about Visual Basic (from beginners stuff through advanced with topics like MTS and ASP). It will gradually expand to include other resources such as free source code and online shopping of development tools. If you are interested in working on a web site dedicated to Visual Basic jump to http://www.allvbstuff.com and fill out the form online. Thanks!


Requirements

In this Issue

In this issue we show how create an add-in for the Visual Basic environment. This is a simple add-in to easily close all opened code and designer windows.

Questions? Email us at questions@codeoftheweek.com.

Close All Windows

The Close All Windows project shows how to create simple Add-Ins for use in the Visual Basic development environment.

To use this class you must add a reference to the Office library and the VB Extensibility library. Refer to the Requirements section above for specific names of these libraries.

We have made the complete project for this issue available online at http://www.codeoftheweek.com/membersonly/bi/issue96

If you have any questions about using this class, let us know at questions@codeoftheweek.com

Important Information

There are not really any methods that need to be discussed to get this project to work. There are many comments explaining the details of the methods which VB calls to make your add-in work.

VB Add-Ins are typically ActiveX DLL's. You will notice in the CloseAllWindows project that the project type selected on the Project Properties option is ActiveX DLL. There is a single class defined called the Connect class. This is the class which the Visual Basic IDE expects to find in an add-in.

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.

Some information which is useful for understanding this issue is the concept of the Implements keyword. To get the add-in to work it must implement all the methods and properties exposed by the IDTExtensibility interface. This interface was written by Microsoft and defines the specific routines that you need to supply in order for your add-in to behave in the VB IDE. When you implement an interface all methods exposed by the interface must be defined. If you do not define all of them your project will not compile successfully.

Returns

See source code.

Sample Usage

This issue is a sample in and of itself. Refer to the source code included and more importantly the complete project at http://www.codeoftheweek.com/membersonly/bi/issue96

Source Code

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

This document is available on the web

Paid subscribers can view this issue in HTML format. There is no additional source or information in the HTML formatted document. It just looks a little better since we have included some HTML formatting. Just point your browser to link at the top of this document.

Other links

Contact Information

C&D Programming Corp.
PO Box 20128
Floral Park, NY 11002-0128
Phone or Fax: (212) 504-7945
Email: info@codeoftheweek.com
Web: http://www.codeoftheweek.com