home *** CD-ROM | disk | FTP | other *** search
/ Using Visual Basic 5 (Platinum Edition) / vb5.iso / Code / ch43 / Connect.cls < prev    next >
Encoding:
Visual Basic class definition  |  1997-07-12  |  2.9 KB  |  97 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "Connect"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = True
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = True
  10. Attribute VB_Description = "Property Code Builder"
  11. Option Explicit
  12.  
  13. Implements IDTExtensibility
  14.  
  15. ' the VBIDE instance connected to this addin
  16. Private VBInstance As VBIDE.VBE
  17. ' the new menu item added to the Add-Ins menu
  18. Private newMenuItem As Office.CommandBarControl
  19. ' the Event object used to get a notification when
  20. ' the user clicks on newMenuItem
  21. Private WithEvents MenuHandler As CommandBarEvents
  22. Attribute MenuHandler.VB_VarHelpID = -1
  23. ' this is the form corresponding to this instance of the class
  24. Dim frmAddin As New frmAddin
  25.  
  26. Private Sub IDTExtensibility_OnConnection(ByVal VBInst As Object, ByVal ConnectMode As vbext_ConnectMode, ByVal AddInInst As VBIDE.AddIn, custom() As Variant)
  27.     
  28.     On Error GoTo OnConnection_Error
  29.     
  30.     'save the VB instance
  31.     Set VBInstance = VBInst
  32.     
  33.     If ConnectMode <> vbext_cm_Startup Then
  34.         ' if no AfterStartup method will be called, this is the right
  35.         ' place to add a menu item to the Add-Ins menu
  36.         CreateMenuItem
  37.     End If
  38.     Exit Sub
  39.     
  40. OnConnection_Error:
  41.     MsgBox "Unable to correctly connect the add-in", vbCritical
  42. End Sub
  43.  
  44. Private Sub IDTExtensibility_OnStartupComplete(custom() As Variant)
  45.     ' if the add-in is being loaded at VB startup, this is
  46.     ' the right place to add a menu item to the Add-Ins menu
  47.     CreateMenuItem
  48. End Sub
  49.  
  50. Private Sub IDTExtensibility_OnDisconnection(ByVal RemoveMode As vbext_DisconnectMode, custom() As Variant)
  51.     On Error Resume Next
  52.     
  53.     'delete the menu item
  54.     newMenuItem.Delete
  55.     ' clear the menu handler object
  56.     Set MenuHandler = Nothing
  57.     
  58.     ' unload the form, if necessary
  59.     Unload frmAddin
  60.     Set frmAddin = Nothing
  61. End Sub
  62.  
  63. Private Sub IDTExtensibility_OnAddInsUpdate(custom() As Variant)
  64.     '
  65. End Sub
  66.  
  67. Sub CreateMenuItem()
  68.     Dim addInMenu As Object
  69.     On Error Resume Next
  70.     
  71.     ' search the Add-Ins menu, exit if not found
  72.     ' (very unlikely)
  73.     Set addInMenu = VBInstance.CommandBars("Add-Ins")
  74.     If (Err <> 0) Or (addInMenu Is Nothing) Then Exit Sub
  75.     
  76.     ' add a new item to the Add-In menu, after existing ones
  77.     Set newMenuItem = addInMenu.Controls.Add(1)
  78.     ' set it caption to a suitable string, with a hotkey
  79.     newMenuItem.Caption = "&Property Builder..."
  80.     
  81.     ' create a menu handler object that will receive
  82.     ' a click event when the user select the menu command
  83.     Set MenuHandler = VBInstance.Events.CommandBarEvents(newMenuItem)
  84.     
  85. End Sub
  86.  
  87. Private Sub MenuHandler_Click(ByVal CommandBarControl As Object, handled As Boolean, CancelDefault As Boolean)
  88.     ' let the form know who is calling it
  89.     Set frmAddin.Connect = Me
  90.     Set frmAddin.VBInstance = VBInstance
  91.     ' show it
  92.     frmAddin.Show
  93. End Sub
  94.  
  95.  
  96.  
  97.