home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Advanced Visual Basic Techniques
(Publisher: John Wiley & Sons, Inc.)
Author(s): Rod Stephens
ISBN: 0471188816
Publication Date: 06/01/97

Search this book:
 
Previous Table of Contents Next


Finally, CreateConnector invokes subroutines CreateConnectAddIn and CreateDisconnectAddIn to create the ConnnectAddIn and DisconnectAddIn subroutines. When these subroutines have finished, CreateConnector closes the file and uses the active project’s AddFile method to add the Connector class module to the project.

Sub CreateConnector()
Dim filename As String
Dim fp As Integer
Dim max_line As Integer

    max_line = AddInDialog.MaxLine
    
    fp = FreeFile
    filename = App.Path & “\Connect.CLS”
    Open filename For Output As fp

    ‘ Write class information.
    Print #fp, “VERSION 1.0 CLASS”
    Print #fp, “BEGIN”
    Print #fp, ”  MultiUse = -1  ‘True“
    Print #fp, ”End”
    Print #fp, “Attribute VB_Name = ”“Connector”“”
    Print #fp, “Attribute VB_Creatable = True”
    Print #fp, “Attribute VB_Exposed = True”
    Print #fp, “Attribute VB_Description = ”“Connector”“”
    
    ‘ Write module global variables.
    Print #fp, “Option Explicit”
    Print #fp, “”
    Print #fp, “‘ The environment instance.”
    Print #fp, “Dim VBInstance As VBIDE.Application”
    Print #fp, “”
    Print #fp, “‘ Submenu lines.”
    Print #fp, “Dim SubmenuLine(0 to” & Str$(max_line - 1) & “) As VBIDE.Submenu”
    Print #fp, “”
    Print #fp, “‘ Menu command lines.”
    Print #fp, “Dim CommandLine(0 to” & Str$(max_line - 1) & “) As VBIDE.MenuLine”
    Print #fp, “”
    Print #fp, “‘ Event connection IDs.”
    Print #fp, “Dim EventID(0 to” & Str$(max_line - 1) & ”) As Long“

    ‘ Create the ConnectAddIn subroutine.
    CreateConnectAddIn fp, max_line

    ‘ Create the DisconnectAddIn subroutine.
    CreateDisconnectAddIn fp, max_line

    Close fp

    VBInstance.ActiveProject.AddFile filename
End Sub

The CreateConnectAddIn and CreateDisconnectAddIn subroutines are described in the following sections.

CreateConnectAddIn

Subroutine CreateConnectAddIn writes the ConnectAddIn subroutine for the Connector class. ConnectAddIn must create the new add-in’s menu entries specified in AddInMaker’s dialog. It uses the choices entered there to decide which entries should be submenus and which should be commands.

AddInMaker allows only one level of submenu. It places command items inside the previous submenu. It places commands that come before any submenu entries in the Add-Ins menu itself.

For command entries, CreateConnectAddIn also writes code to connect the command to an event handler class. It uses the class name specified in the dialog. The new add-in’s ConnectAddIn code creates an object of the appropriate class and connects the object to the menu command, saving the event handler ID for later use by DisconnectAddIn.

Sub CreateConnectAddIn(fp As Integer, max_line As Integer)
Dim line_caption As String
Dim line_class As String
Dim prev_menu As Integer
Dim i As Integer
    
    Print #fp, ”Sub ConnectAddIn(vb_instance As VBIDE.Application)”
    Print #fp, “Dim obj As Object”
    Print #fp, “”
    Print #fp, “    ‘ Save the Visual Basic instance for later.”
    Print #fp, “    Set VBInstance = vb_instance”
    Print #fp, “”
    Print #fp, “    ‘ Create the submenus and commands.”
    prev_menu = -1
    For i = 0 To max_line - 1
        line_caption = Trim$(AddInDialog.CaptionText(i).Text)
        If AddInDialog.SubmenuCheck(i).value = vbChecked Then
            ‘ Create a new submenu.
            Print #fp, “    Set SubmenuLine(” & _
                Str$(i) & _
                “) = VBInstance.AddInMenu.MenuItems.AddMenu(”“” & _
                line_caption & “”“)”
            prev_menu = i
        Else
            ‘ Create a new command.
            line_class = Trim$(AddInDialog.ClassNameText(i).Text)
            If prev_menu < 0 Then
                ‘ Put it directly in the Add-Ins menu.
                Print #fp, “    Set CommandLine(” & _
                    Str$(i) & _
                    “) = VBInstance.AddInMenu.MenuItems.Add(”“” & _
                    line_caption & “”“)”
            Else
                ‘ Put it under the last submenu.
                Print #fp, “    Set CommandLine(” & _
                    Str$(i) & “) = SubmenuLine(” & _
                    Format$(prev_menu) & _
                    “).MenuItems.Add(”“” & _
                    line_caption & “”“)”
            End If
            
            ‘ Connect the event handler.
            Print #fp, “    Set obj = New ” & _
                line_class
            Print #fp, “    Set obj.VBInstance = VBInstance”
            Print #fp, _
                “    EventID(” & Str$(i) & “) = CommandLine(” & _
                Str$(i) & “).ConnectEvents(obj)”
        End If
        Print #fp, “”
    Next i
    Print #fp, “End Sub”
End Sub

CreateDisconnectAddIn

Subroutine CreateDisconnectAddIn writes the DisconnectAddIn subroutine. DisconnectAddIn uses the event IDs saved by ConnectAddIn to remove the connection between commands in the Add-Ins menu and the event handler objects that respond to them. It then removes the new add-in’s submenus and menu commands from the Add-Ins menu.

Sub CreateDisconnectAddIn(fp As Integer, max_line As Integer)
Dim line_caption As String
Dim line_class As String
Dim prev_menu As Integer
Dim i As Integer
    
    Print #fp, “Sub DisconnectAddIn(mode As Integer)”
    Print #fp, “    ‘ Remove the submenus and commands.”
    
    For i = max_line - 1 To 0 Step -1
        If AddInDialog.SubmenuCheck(i).value = vbUnchecked Then
            ‘ It‘s a command line.
            ‘ Disconnect the event handler.
            Print #fp, “    CommandLine(” & _
                Str$(i) & “).DisconnectEvents EventID(” & _
                Str$(i) & “)”
            
            ‘ Find the submenu holding the command.
            For prev_menu = i - 1 To 0 Step -1
                If AddInDialog.SubmenuCheck(prev_menu).value = _
                    vbChecked Then Exit For
            Next prev_menu
            
            ‘ Remove the command.
            If prev_menu < 0 Then
                ‘ The command is directly in the
                ‘ Add-Ins menu.
                Print #fp, _
    “    VBInstance.AddInMenu.MenuItems.Remove CommandLine(” & _
                    Str$(i) & “)”
            Else
                ‘ The command is in the submenu.
                Print #fp, “    SubmenuLine(” & Str$(prev_menu) & _
                    “).MenuItems.Remove CommandLine(” & Str$(i) & “)”
            End If
            Print #fp, “    Set CommandLine(” & _
                Str$(i) & “) = Nothing”
        Else
            ‘ It‘s a submenu.
            Print #fp, _
    “    VBInstance.AddInMenu.MenuItems.Remove SubmenuLine(” & _
                Str$(i) & “)”
            Print #fp, “    Set SubmenuLine(” & _
                Str$(i) & “) = Nothing”
        End If
        Print #fp, “”
    Next i
    Print #fp, “End Sub”
End Sub

Creating Command Classes

The new add-in’s ConnectAddIn subroutine creates event handler objects for the menu commands specified in AddInMaker’s dialog. Subroutine CreateClasses uses the CreateClass subroutine to define each of the event handler classes.

Sub CreateClasses()
Dim i As Integer

    For i = 0 To AddInDialog.MaxLine - 1
        If AddInDialog.SubmenuCheck(i).value = vbUnchecked Then
            CreateClass Trim$(AddInDialog.ClassNameText(i).Text)
        End If
    Next i
End Sub

CreateClass builds a simple class with the name given by its parameter. It begins by writing information into a new class module to identify the module as a class definition. CreateClass then gives the class a simple AfterClick subroutine. Initially AfterClick presents a message box that acts as a place holder for whatever code the developer wants to insert. When it finishes, CreateClass adds the new module to the project.

Sub CreateClass(class_name As String)
Dim filename As String
Dim fp As Integer

    On Error GoTo CreateClassError
    fp = FreeFile
    filename = App.Path & “\” & class_name & “.CLS”
    Open filename For Output As fp

    ‘ Class information.
    Print #fp, “VERSION 1.0 CLASS”
    Print #fp, “BEGIN”
    Print #fp, “  MultiUse = -1  ‘True”
    Print #fp, “End”
    Print #fp, “Attribute VB_Name = ”“” & class_name & “”“”
    Print #fp, “Attribute VB_Creatable = True”
    Print #fp, “Attribute VB_Exposed = True”
    Print #fp, “Attribute VB_Description = ”“” & class_name & “”“”
    
    ‘ Module global variables.
    Print #fp, “Option Explicit”
    Print #fp, “”
    Print #fp, “‘ The environment instance.”
    Print #fp, “Public VBInstance As VBIDE.Application”

    ‘ Create the AfterClick subroutine.
    Print #fp, “Public Sub AfterClick()”
    Print #fp, _
        “    MsgBox ”“Insert code for ” & class_name & “ here.”“”
    Print #fp, “End Sub”
    
    Close fp

    VBInstance.ActiveProject.AddFile filename
    Exit Sub
End Sub

Summary

Add-ins are designed to automate simple, detailed tasks. Building an add-in is itself a simple but detailed task, so it makes sense to create an add-in that builds other add-ins. AddInMaker does just that. Using AddInMaker a developer can easily create the framework needed to implement a new add-in.

AddInMaker can create multiple add-in submenus, each containing one or more commands. A more elaborate program could use similar techniques to create an even more complex series of cascading add-in menus.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.