Visual Basic Concepts

Manipulating Code with Add-Ins

See Also

The Visual Basic extensibility model provides you with the ability to view and manipulate code in Visual Basic projects through the CodeModule object. Using the CodeModule object, you can programmatically add or remove code to or from a project, search through code, or alter it. You can browse code to find out how many total lines it has, or in a particular procedure, what the starting and ending lines are, and so on. You can also add, delete, or replace lines of code.

Important   You should realize the distinction between the CodeModule and CodePane objects. The CodePane object only allows you to view code, but not alter it. The CodeModule object allows you to alter code, but not view it. The two objects work together to allow you to both view and alter code.

The Members object allows you to view and manipulate code and attributes for procedures in a given module. The attributes pertaining to each procedure change depending on the type of module and procedure you're viewing. To see the attributes of a procedure, choose Procedure Attributes from the Tools menu.

A designer is not an object per se; it's really a base on which you create your visual interface. You can think of it in the extensibility model as an empty socket which you can plug in different designers. For example, if you are using Visual Basic forms, whenever you create or edit a form, you use the Visual Basic forms designer. If you are creating or editing a UserDocument, you are using the UserDocument designer. When creating or editing a UserControl, you are using the UserControl designer. While designers are programmatically different from each other, though, they may or may not have any visual differences to the user.

The following code fragments demonstrate how to reference the CodeModule object:

' Clear the code module of all text
Private Sub cmdClearText_Click()
   Dim p As VBProject
   Dim c As VBComponent
   Dim sc As String
   Dim sp As String
   
   Screen.MousePointer = vbHourglass
   sp = cmbProj.Text
   sc = cmbComp.Text
   If sp <> "" And sc <> "" Then
      Set c = _
         vbi.VBProjects.Item(sp).VBComponents.Item(sc)
      c.CodeModule.DeleteLines 1, _
         c.CodeModule.CountOfLines
      SynchCodePaneScroll
   End If
   Screen.MousePointer = vbDefault
End Sub

' Insert text into code module.
Private Sub cmdInsertText_Click()
   Dim p As VBProject
   Dim c As VBComponent
   Dim sc As String
   Dim sp As String
   
   Screen.MousePointer = vbHourglass
   sp = cmbProj.Text
   sc = cmbComp.Text
   If sp <> "" And sc <> "" And txtDisplay.Text _
      <> "" Then
      Set c = _
         vbi.VBProjects.Item(sp).VBComponents.Item(sc)
      c.CodeModule.AddFromString txtDisplay.Text
      SynchCodePaneScroll
   End If
   Screen.MousePointer = vbDefault
End Sub

' Sync up the position of the scrollbar control with 
' current CodePane position.
Private Sub SynchCodePaneScroll()
   vbi.VBProjects.Item(sp).VBComponents.Item(sc)
   Dim cp As CodePane
      Set cp = GetCodePane
      sclCodePane.Max = cp.CodeModule.CountOfLines
      sclCodePane.Value = cp.TopLine - 1
End Sub