Reflection Desktop VBA Guide
HowTos / Import Macros To the Common Project
In This Topic
    Import Macros To the Common Project
    In This Topic

     

    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.

     Sharing Macros in the 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.

    1. Save the macros in a module in the Common project and export the module.
    2. Create a macro that updates your shared code and set up Reflection to run the macro when the workspace opens.
    3. Distribute the workspace settings and the vbaProject.bin file that contains the startup macro.

    Save the Macros in a module in the Common project and export the module

     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:

    To add a startup macro that imports macros into the Common project

    1. Create your macros and save them in a module in the Common project. For example, the SharedCode module that was created in the Common Project contains the CheckTLSVersion macro.
    2. Right click on the module and select Export to File.
       
    3. Save the module as a .bas file.

    Create a macro that updates your shared code and set up Reflection to run the macro when the workspace opens

    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.

     

    To add a startup macro that imports macros into the Common project

    1. Create a new module in the Reflection VBA Common project. (In our examples above we created a module named Import.) 
    2. Add the following code to the new code 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
      
    3. In the Reflection Workspace Settings window, click Configure Workspace Defaults.
    4. In the When starting workspace list, select Run Startup action. and then click Select Action.
    5. In the Run Startup Action dialog box, in the Action list, select Run Reflection Workspace Macro.
    6. Click Select macro and then in the Select a VBA project list, select Common.
    7. Under Select a macro, select the macro you created.

    Distribute the workspace settings and the vbaProject.bin file that contains the startup macro

    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.

    1. Distribute the application.settings and Frame.settings files that contain the workspace settings to run the macro and the vbaProject.bin file that contains the startup macro to users. These files must be distributed to the %appdata%/Micro Focus/Reflection/Desktop/v17.0 folder, as shown in the  Reflection Deployment Guide.
    2. Save the .bas file in the location that you specified in the startup macro.