Using the Extensibility object model and add-ins, you can greatly extend the Visual Basic development environment. You can also create ActiveX documents to extend the Visual Basic IDE. Develop your ActiveX document as you would any other, but use the CreateToolWindow function to create a tool window in the IDE which will contain the ActiveX document.
Note This topic assumes you have some knowledge of add-ins — what they are, and how to create them. If you are unfamiliar with add-ins, see "Add-Ins Overview," and "Creating a Basic Add-In," in "Extending the Visual Basic Environment with Add-Ins."
Using the CreateToolWindow function has the following advantages over other add-ins:
The CreateToolWindow function is a method of the Windows collection. The function creates a tool window — a container for the ActiveX document — and returns a reference to the window. The following series of steps outline what happens in a typical scenario:
To create a simple Add-in using an ActiveX document
Implements IDTExtensibility
Public FormDisplayed As Boolean
Public VBInstance As VBIDE.VBE
Public mcbMenuCommandBar As Office.CommandBarControl
Dim frmAddIn As New frmAddIn
Public WithEvents MenuHandler As CommandBarEvents
' Add the following declarations for the
' CreateToolWindow function:
Private mWin As Window
Private mobjDoc As axdUserDoc
Const guidMyTool$ = "(4244B234-E45F-12dg-8O3f-04884)"
The code you added declares the necessary object variables for the CreateToolWindow function. The Window variable "mWin" will be set to the reference returned by the function; you can then use this reference to show or hide the window. The second variable, "mobjDoc," will be set to reference the UserDocument. You can then use this reference to manipulate the ActiveX document. Finally, the constant "guidMyTool" is used by the function to identify the window instance; for every tool window you create, this string must be unique.
Private Sub IDTExtensibility_OnConnection(ByVal VBInst As Object, ByVal ConnectMode As VBIDE.vbext_ConnectMode, ByVal AddInInst As VBIDE.AddIn, custom() As Variant)
Set mWin = AddInInst.VBE.Windows. _
CreateToolWindow(AddInInst, _
"MyAddin.axdUserDoc", _
"ActiveX Caption", guidMyTool, mobjDoc)
mWin.Visible = True
End Sub
This code uses the Set statement to set the Window variable "mWin" to a reference to the window created by the function. This reference is then used to set the Visible property of the window to True.
Having an understanding of the mechanics of the function allows us to create a more complex scenario: creating a second window, containing a second ActiveX document, from the first.
To add a second ActiveX document
Public Property Get Text () As String
Text = Text1.Text
End Property
Public Property Let Text(ByVal newText As String)
Text1.Text = newText
End Property
The preceding code creates a public property, which you will set when you create the window for the document.
Public gAddIn As vbide.Addin
The new line declares a global variable that will be set with a reference to the add-in instance.
Private Sub IDTExtensibility_OnConnection(ByVal _
VBInst As Object, ByVal ConnectMode As _
VBIDE.vbext_ConnectMode, ByVal AddInInst As _
VBIDE.AddIn, custom() As Variant)
Set mWin = AddInInst.VBE.Windows. _
CreateToolWindow(AddInInst, _
"MyAddin.axdUserDoc", _
"ActiveX Caption", guidMyTool, mobjDoc)
mWin.Visible = True
' Add this new code:
Set gAddIn = AddInInst
End Sub
The new line sets the global variable to the add-in instance, making it available to create more windows.
Private mWin2 As Window
Private mDoc2 As axdUserDoc2
Const guidMyTool$ = "Xiang19X67Hangzhou4/27"
Private Sub Command1_Click()
set mWin2 = gAddIn.VBE.Windows. _
CreateToolWindow(gAddIn, _
"MyAddin.axdUserDoc2", _
"Second ActiveX Document", _
guidMyTool, mDoc2)
mWin2.Visible = True
mDoc2.Text = "This is the second document."
End Sub