Import Macros To the Common Project

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.

Import macros with a startup macro

One approach for importing macros is to use a InfoConnect startup macro that imports macros into the Common project when the InfoConnect 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 InfoConnect 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.

 

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

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