You can update custom code that is shared by a group of users by setting up Reflection to regularly update a code module in the VBA Common project.
Using the Common project
Macros in the VBA Common project are available to all session documents. This is a useful place to save utility macros, macros that control the workspace (Frame object) settings, and other macros that are frequently used.
Macros in the Common project can be called from any session code modules. For example, you could call the CopyScreenTextToClipboard macro from one of your session macros as follows:
'Call a macro in the Common project from a session macro Sub UseAMacroInTheCommonProject() Call CopyScreenTextToClipboard End Sub
Problems with distributing updates to code shared by a group of users
The macros in the Common project are saved in the vbaProject.bin file. If you provide a group of users with a vbaProject.bin file that includes custom macro code for the Common project, and you later need to update this code, you can simply provide an updated vbaProject.bin file that includes the updated code to everyone.
However, if you use this approach, you'll have to provide the file to users each time you update the code and any macros that users have saved locally in the Common project are lost.
Benefits of importing code modules
You can simplify administration and avoid overwriting users' macros when you update your shared code by setting up Reflection to automatically import a custom code module into the Common project when the workspace opens. When you use this approach, everyone receives updates on a regular basis without losing their own code.
This walkthrough shows how to set up Reflection to run a macro that imports your custom code module from a central location each time the workspace is opened. To use this approach for updating your code, you'll need to set up several settings when you distribute the macros the first time.
First, you'll need to save the macros you want to distribute in a module in the Common project and then export the module to a .bas file as follows:
One approach for importing a macro .bas file 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 also 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 'Replace with your share location FileSpec = "\\share\SharedCode.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 "SharedCode" 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 = "SharedCode" Else Debug.Print FileSpec + " does not exist." End If End Sub |
You'll need to distribute the workspace settings and the vbaProject.bin file to users and make sure the .bas file you created is saved in the location you referenced in the startup macro.