Visual Basic Concepts

Manipulating the IDE with Add-Ins

See Also

Several objects in the extensibility object model allow you to manipulate and extend the Visual Basic IDE. The code in this topic illustrates a few examples.

Commandbars Collection and CommandBar Object

These allow you to manipulate command bars, which are a meld of toolbars and menu bars. You can do nearly anything to a CommandBar object, including creating new ones, deleting existing ones, and changing their size, location, and icons.

Here is an example of how to manipulate command bars:

Dim mcbMenuCommandBar As Office.CommandBarControl

' Make sure the Standard toolbar is visible.
gVBInstance.CommandBars("Standard").Visible = True
' Add as button to the Standard toolbar.
' This adds the button to the right of the Toolbox 
' button.
Set mcbMenuCommandBar = _
gVBInstance.CommandBars("Standard").Controls.Add _
(1, , , gVBInstance.CommandBars("Standard") _
.Controls.Count)
' Set the caption of the button.
mcbMenuCommandBar.Caption = "My Test"
' Copy an icon to the clipboard.
Clipboard.SetData LoadPicture("c:\windows\circles.bmp")
' Paste the icon on the button.
mcbMenuCommandBar.PasteFace

' Handle the CommandBarEvents object.
Set Me.MenuHandler = _
gVBInstance.Events.CommandBarEvents _
(mcbMenucommandBar)

For More Information   For additional information on using CommandBars, see Chapter 4, "Creating a Toolbar Button for Add-In Activation."

Windows Collection and Window Object

These allow you to create and delete windows, as well as move and size windows open in the IDE. All windows are contained in the Windows collection. This means that these windows are never really closed, but rather their visibility is altered with the Visible property.

Note that while you can use the Window object to manipulate a window frame itself, you cannot use it to manipulate the objects contained in a window (such as controls). To do that, you must reference the appropriate object.

The following code fragment demonstrates how to reference the Window object and Windows collection:

' Toggle window visibility. Assumes ordering of 
' windows in lists matches ordering of windows in 
' collection. A better method is to look up window by 
' caption and type.
Private Sub cmdToggle_Click()
   Dim w As Window
   Dim sw As String

   sw = Combo1.Text
   If sw <> "" Then
      If sw = "MainWindow" Then
         Set w = vbi.MainWindow
      Else
         Set w = vbi.Windows(Combo1.ListIndex)
      End If
      w.Visible = Not w.Visible
      RefreshWindows
   End If
End Sub

LinkedWindows Collection

This contains all of the panes in a given LinkedWindowFrame. A "linked window" is defined as two or more windows which are docked together into a single, joint window surrounded by a frame known as a "LinkedWindowFrame." Each sub-window in the LinkedWindowFrame is known as a "pane". LinkedWindowFrames exist around all windows that can be linked or docked together, with the exception of code windows, designer windows, the Object Browser window, and the Search and Replace window.

If you remove all panes from a LinkedWindowFrame, it is deleted. (The sole exception is the Main window.)

Only windows of type LinkedWindowFrame have a LinkedWindows collection. For other window types, this collection is Nothing.

The code fragment below demonstrates how to reference the LinkedWindows collection:

' Unlink or undock window if it is linked or docked.
Private Sub cmdUnlinkWindow_Click()
   Dim w As Window
   Dim sw As String

   sw = Combo1.Text
   If sw = "" Then Exit Sub
   If sw = "MainWindow" Then
      Set w = vbi.Windows.MainWindow
   Else
      Set w = vbi.Windows(Combo1.ListIndex)
   End If
   If Not w.LinkedWindowFrame Is Nothing Then
      w.LinkedWindowFrame.LinkedWindows.Remove w
   End If
   RefreshWindows
End Sub

CodePane Object

This is a window provided by an object which visually displays the object's code. You cannot create or destroy CodePane objects in the IDE, but you can manipulate their height, width, and location. You can also insert or delete lines of code (using the InsertLines and DeleteLine methods), as well as select certain lines and make the highlight visible or not. You can use the GetSelection method to copy selected code into the Windows clipboard.

While the CodePane object allows you to visually examine or select existing code, it doesn't allow you to alter it. To do this, you must use the CodeModule object, as described in "Manipulating Code with Add-Ins" in this chapter.

The following code fragment demonstrates how to reference the CodePane object:

' Scroll codepane using scrollbar.
Private Sub sclCodePane_Change()
   Dim p As VBProject
   Dim c As VBComponent
   Dim cp As CodePane
   Dim sc As String
   Dim sp As String

   sp = cmbProj.Text
   sc = cmbComp.Text
   If sp <> "" And sc <> "" Then
      Set c = _
         vbi.VBProjects.Item(sp). _
         VBComponents.Item(sc)
      Set cp = c.CodeModule.CodePane
      cp.TopLine = sclCodePane.Value + 1
   End If
End Sub