Visual Basic Concepts

Manipulating Projects with Add-Ins

See Also

This group of objects allow you to manipulate projects — that is, add or remove projects to or from an existing project group. Also, you can add or remove VBComponents to or from projects. A VBComponent is any object that can be added to a Visual Basic project, such as forms, controls, code modules, and so forth.

The following code fragments demonstrate how to reference the VBProject object and VBProjects collection, as well as other extensibility objects:

' Create new project.
Private Sub cmdCreateNew_Click()
   Dim p As VBProject
   Set p = vbi.VBProjects.Add _
      (cmbProjKind.ItemData(cmbProjKind.ListIndex))
   If txtProjName.Text <> "" Then
      p.Name = txtProjName.Text
   End If
End Sub

' Activate component.
Private Sub cmdActivate_Click()
   Dim sc As String
   Dim sp As String
   Dim c As VBComponent

   sp = cmbProj.Text
   sc = cmbComp.Text
   If sp <> "" And sc <> "" Then
      Set c = _
         vbi.VBProjects.Item(sp).VBComponents.Item(sc)
      c.Activate
   End If
End Sub

' Create new component of the type indicated in the 
' cmbCompKind combo.
Private Sub cmdCreateNewVBComponent_Click()
   Dim p As VBProject
   Dim c As VBComponent
   Dim sp As String
   sp = cmbProj.Text
   If sp <> "" Then
      Set p = vbi.VBProjects.Item(sp)
      Set c = p.VBComponents.Add _
         (cmbCompKind.ItemData(cmbCompKind.ListIndex))
   End If
End Sub

' Get the text from the code module and display in text 
' control.
Private Sub cmdGetText_Click()
   Dim i As Long
   Dim str As String
   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)
      txtDisplay.Text = ""  ' Clear the text control
      For i = 1 To c.CodeModule.CountOfLines
         str = str & c.CodeModule.Lines(i, 1) & CRLF
      Next i
      txtDisplay.Text = str
      SynchCodePaneScroll
   End If
   Screen.MousePointer = vbDefault
End Sub

' Refresh the list of projects. Called from events in 
' the main object.
Public Sub RefreshProjects()
   Dim p As VBProject
   Dim tempIndex As Long

   Screen.MousePointer = vbHourglass
   If cmbProj.ListCount > 0 Then
      tempIndex = cmbProj.ListIndex 
      ' Temp index to restore prior selection.
   End If
   cmbProj.Clear
   For Each p In vbi.VBProjects
      cmbProj.AddItem p.Name
   Next p
   ' Restore prior selection.
   If cmbProj.ListCount > 0 Then
      If tempIndex <= cmbProj.ListCount - 1 Then
      cmbProj.ListIndex = tempIndex
      Else
         cmbProj.ListIndex = 0
      End If
   End If
   Screen.MousePointer = vbDefault
End Sub