Programmatically importing macros into the the VBA Common project enables you to add macros to this project without overwriting existing macros. This offers an advantage over deploying macros in the vbaProject.bin file, because that process removes all existing macros created locally by users in the Common project.
One approach for importing macros is to use a Reflection startup macro that imports macros into the Common project when the Reflection Workspace opens, before any sessions are opened.
If you use this approach, you'll need to set up the macro to remove previous versions of the macro module to avoid cluttering the project with obsolete modules. (The startup macro runs every time Reflection is opened and imported macros are automatically renamed when they are added to the project.)
If you have macros in session files that call the macros in the common module, you'll need to rename the imported macro module so that it retains its original name.
The following sample shows how to determine if a new module is available, and how to remove the old module, import the new module, and rename the imported module.
Import a module into a VBA macro |
Copy Code
|
---|---|
Sub ImportMacrosToCommonProject() Dim Count As Integer Dim FileSpec As String FileSpec = "C:\test\myModule.bas" If VBA.Len(VBA.Dir(FileSpec)) > 0 Then 'If the module is already in the Common Project, remove the existing module Count = ThisFrame.VBCommonProject.VBComponents.Count For I = 1 To Count If ThisFrame.VBCommonProject.VBComponents.Item(I).Name Like "myModule" Then ThisFrame.VBCommonProject.VBComponents.Remove ThisFrame.VBCommonProject.VBComponents.Item(I) End If Next I 'Import the new module and rename it so it matches the name referenced by other macros ThisFrame.VBCommonProject.VBComponents.import FileSpec Count = ThisFrame.VBCommonProject.VBComponents.Count ThisFrame.VBCommonProject.VBComponents.Item(Count).Name = "myModule" Else Debug.Print FileSpec + " does not exist." End If End Sub |