title: VBA - Getting the modules in workbook
body: <p>I'm trying to create a workbook that is used for creating other .xlsm workbooks, but can't figure out how to get the modules I need so I can add them.</p>
  
  <p>My code as it stands is  below (modified from the answer given here: <a href="https://stackoverflow.com/questions/14493473/how-to-add-excel-2010-macro-programmatically">How to add excel 2010 macro programmatically</a>)</p>
  
  <p>The place I need help is in the ImportModules sub, by the comment <code>'LIST MODULES HERE</code></p>
  
  <p>How can I get an array of modules that are in the current workbook?</p>
  
  <pre><code>Private Sub SVAmaker_Click()
  
      Dim file As String
      file = InputBox("SVA Planner file name", "Name", "Name")
  
      Application.DefaultSaveFormat = xlOpenXMLWorkbookMacroEnabled
      Workbooks.Add
      ActiveWorkbook.SaveAs filename:=file
  
      Dim WB As Workbook
      WB = ActiveWorkbook
      Call ImportModules(VBA.CStr(WB))
  
  End Sub
  
  Sub ImportModules(sWorkbookname As String)
  
      Dim cmpComponents As VBIDE.VBComponents
      Dim wbkTarget As Excel.Workbook
  
      Set wbkTarget = Workbooks.Open(sWorkbookname)
  
      If wbkTarget.VBProject.Protection = 1 Then
          Debug.Print wbkTarget.Name &amp; " has a protected project, cannot import module"
      GoTo Cancelline
      End If
  
      Set cmpComponents = wbkTarget.VBProject.VBComponents
  
      Dim vModules As Variant
      'LIST MODULES HERE
  
      Dim i As Integer
      For i = LBound(vModules) To UBound(vModules)
          cmpComponents.Import vModules(i)
      Next i
  
  Cancelline:
  
      If wbkTarget.FileFormat = xlOpenXMLWorkbook Then
          wbkTarget.SaveAs wbkTarget.Name, xlOpenXMLWorkbookMacroEnabled
          wbkTarget.Close SaveChanges:=False
      Else
          wbkTarget.Close SaveChanges:=True
      End If
  
      Set wbkTarget = Nothing
  
  End Sub
  </code></pre>