Extending product function with VBA > Automation and System Architect > Automation of macro projects with VBE
  
Automation of macro projects with VBE
Access projects
Sub VBA_VBE_AccessProjects()
Dim mp As MacroProject
Dim strTxt As String

For Each mp In Application.HostContainer.MacroProjects
strTxt = strTxt & Print mp.ProjectName
' mp.TheProjectReference has some members RE the Microsoft APC library
' mp.TheProjectReference.VBProject has some members RE the Visual Basic for Applications Extensibilty library
Next
MsgBox strTxt
End Sub
Create projects and import code
' requires reference to Microsoft APC 6.3 Object Library
Sub VBA_VBE_ImportProjectCode()
Dim MainProject As MacroProject
Dim VBAProject As Project
Dim strMacroName As String

Set MainProject = New MacroProject
' project file to create / overwrite
strMacroName = "c:\temp\test2.mac" ' do not have an existing macro loaded called 'test2' or the imported file will be added to that
' Initialize returns False if initialization fails...
If MainProject.Initialize(strMacroName, MSAPC.axAccessReadWrite Or MSAPC.axAccessExclusive, MSAPC.axProjectNormal Or MSAPC.axProjectThrowAwayCompiledState, False, True, True) = False Then
' Initialization failed... (so delete the object)
MsgBox "Error creating project " & strMacroName, vbOKOnly
Else
' Initialization succeeded, so we can add the project to the collection...
HostContainer.MacroProjects.Add MainProject, strMacroName 'DEV_STRING
Set VBAProject = MainProject.TheProjectReference

' ..and import code
Call VBAProject.Apc.VBE.VBProjects(VBAProject.name).VBComponents.Import("c:\temp\test.bas")
' needs to have a routine in it or you get an 'input past end of file' message

' and save..
VBAProject.Save
End If
End Sub
Export code from current project
' requires ref to Microsoft Visual Basic for Applications Extensibilty 5.3
Public Sub VBE_VBA_ExportAllCode()
On Error GoTo HandleError
Dim VBAEditor As VBIDE.VBE
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim filename As String
Const path As String = "c:\temp\files"
' get the VBE object
Set VBProj = Application.HostContainer.Host.VBE.VBProjects("sample") ' get the project
For Each VBComp In VBProj.VBComponents ' for each component
filename = path & "\" & VBComp.name ' set the filename
If VBComp.Type = vbext_ct_ClassModule Then
filename = filename & ".cls" 'DEV_STRING
ElseIf VBComp.Type = vbext_ct_StdModule Then
filename = filename & ".bas" 'DEV_STRING
ElseIf VBComp.Type = vbext_ct_ActiveXDesigner Then
filename = filename & ".frm" 'DEV_STRING
ElseIf VBComp.Type = vbext_ct_MSForm Then
filename = filename & ".frm" 'DEV_STRING
End If
Call VBComp.Export(filename) ' export the component
Next
GoTo Finish
HandleError:
MsgBox Err.Description
Finish:
Set VBComp = Nothing
Set VBProj = Nothing
Set VBComp = Nothing
End Sub
See also
Automation
Customizable solutions
Planning an automated solution with System Architect
Automation and System Architect