Skip to content

Record, Run, and Edit VBA Macros

You can create and run Visual Basic for Applications (VBA) macros to simplify and automate routine tasks.

Running VBA Macros

You can run VBA macros saved in your session document file from the Macro Panel. This is a convenient way to keep track of and run your macros without adding buttons to the ribbon. You can also run other types of macros from this panel after you run them from the Run Macro dialog box.

Running macros with the Macro Panel

You can use the Macro Panel to run VBA macros saved in your session document file or to run other macros, such as referenced macros, EML macros, or macros created with other products, that you have previously run on that session. The Macro Panel is a convenient way to keep track of and run macros that apply to a session without adding buttons to the ribbon. It displays only the macros that apply to the session that has focus.

Use the Macro Panel

To run macros from the Macro Panel

  1. On the Session tab, in the Macros group, click Show Macro Panel.

    The Macro Panel has three tabs:

    The Available tab shows the VBA macros that are saved in the session document file.

    note

    The Available tab does not show EML macros, macros created with other products, or other macros that are not saved in the session document file.

    The Recent tab shows all macros that you have previously run in this session. This includes VBA macros as well as other supported macros that you have run, such as EML macros or macros created with other products.

    The Favorites tab shows the all the macros that you have marked as favorites.

  2. To run a macro, mouse over the macro you want to run and then the Play button on the left side of the macro.

  3. To stop a macro, click the Stop button next to the macro.

  4. To get more information about a macro, mouse over the macro.

    For VBA macros, a tooltip shows the VBA project and module that the macro is in.

    For other macros, a tooltip shows the type of macro and its fully qualified file name.

Running macros from the Run Macro dialog box

You'll need to use the Run Macro dialog box to run macros that are not saved in the session document file the first time you run these macros. After you run a macro once, you can run it from the Macro Panel's Recent tab.

To run a macro from the Run Macro Dialog Box

  1. Open the Run Macro dialog box. The steps depend on your user interface mode.

    User Interface Mode Steps
    Ribbon On the Session tab, from the Macros group, click Run Macro.
    Classic and Classic MDI On the Macro menu, choose Macros.
  2. Select the type of macro you want to run and select to open it.

    The macro runs.

    note

    After you run the macro once, you can run it from the Macro Panel's Recent tab.

Running macros created in other products

You can run VBA macros created in Reflection, and most macros created with Extra! and legacy Reflection products. You can also run the majority of macros created with the Open Text Rumba, IBM Personal Communications, OpenText HostExplorer, and Brandon Systems/Jolly Giant QWS 3270 products. However, you can only run macros in trusted locations.

Running Extra! Macros in Reflection Desktop

Several macro-related actions (such as, Run a Reflection Workspace Macro) are available from the Select Action dialog box.

You can also run a macro by mapping an action to a control. For more information, see Add a Button to Run a Macro.

To run macros created with other products

  1. Open the Run Macro dialog box. The steps depend on your user interface mode.

    User Interface Mode Steps
    Reflection Ribbon On the Macros tab, from the Advanced group, click Run Macro.
    Reflection Browser On the Reflection menu, choose Tools, Macro and then Run Macro.
    TouchUx Tap the Wrench icon and then under Macro, select Show Run Macro Dialog.
  2. From the Run Macro dialog box, select the macro you want to run.

    Select To...
    Reflection Workspace Macro Show macros in the active session document. Select to run macros created in Reflection session documents.
    Run a macro in the Common project and other documents, select the option from the Macros in menu:
    Legacy Reflection Macro in This File Run legacy macros in the active document. These macros include legacy settings files (.rsf, .r2w, or .r4w), and in Reflection, session documents (.rd3x, .rd5x, .rdox) that were previously converted from settings files.
    Legacy Reflection Macro in Another File Specify and run a macro in a SharedMacro file (.rma) or a settings file (.rsf, .r2w, or .r4w).
    Legacy RB Macro Specify and run a Reflection Basic macro (.rbs).
    Legacy RCL Script Specify and run a legacy RCL script. Available only for use in VT session documents.
    Extra! Macro Specify and run an Extra! macro file (.ebm).
    Rumba Macro Specify and run a Open Text Rumba macro file (.rmc).
    QWS Macro Specify and run a Brandon Systems\Jolly Giant QWS3270 macro file (.jgs).
    IBM Personal Communications Macro Specify and run an IBM Personal Communications macro file (.mac).

    note

    Legacy macro options are available only if compatibility features are installed. In addition, to run Extra! and legacy Reflection macros from external files, you must specify, from the Set Up API and Macro Security dialog box, the type of legacy macro you want to run.

  3. If you can't find a Workspace macro, select Set Up VBA References in the Settings window to open the Add VBA Reference macro dialog box and then check the list for missing references (indicated by the button). After you correct the problem (usually a missing file or a disconnected network drive), close and reopen your session.

Recording and Deleting VBA Macros

You can record a VBA macro to automate your interaction with host applications, including:

  • Sending data to, or typing text in, a host application.

  • Cutting, copying, or pasting text or data from one host application to another.

  • Switching tabs to move from one host application to another.

  • Selecting text with a mouse or clicking the mouse to move the cursor (mouse clicks are recorded as cursor positions.)

You cannot record:

  • Interaction with Reflection settings and Productivity features (such as Spell Check, Auto Expand, and Auto Complete).

  • Connecting to or disconnecting from a host.

  • Interaction with Web applications.

  • Cutting or pasting from a host to an external application (for example, Notepad).

How to Record a Macro When Using the Reflection Desktop Ribbon

How to Record a Macro When Using the Reflection Desktop Classic Interface

Best Practices for Recording Macros

Use the following best practices to carefully record your macro will help prevent problems that can occasionally occur when using a slower network connection. Typing very fast while recording or "typing ahead" in a session, while connected with a slow network connection can cause your macro to play back in unexpected ways. Following these best practices will lead to the best results.

  • Plan in advance of the recording to make sure you know the steps you will follow, which keys you will press, and which host screens are anticipated.

  • While recording the macro, after pressing a key to submit data to the host, wait for the next host screen to fully appear (sometimes even waiting a few extra seconds) before pressing the next keys.

  • If your macro doesn't play back like you expect, delete the macro and carefully record it again using slow and deliberate steps.

Recording a macro slowly and carefully does not cause the macro to run with reduced performance, as macros always run at the fastest speed possible during playback. Recording a well thought out and planned macro will lead to the best results.

To record a macro

  1. Select to record a macro.The steps depend on your user interface mode.

    User Interface Mode Steps
    Ribbon On the Macros tab, from the Advanced group, click Record VBA.
    Reflection Browser On the Reflection menu, choose Tools, Macro and then Record VBA Macro.
    TouchUx Tap the Wrench icon, and then under Macro, select Record VBA Macro.
  2. Perform the task(s) that you want to automate.

  3. (Optional) If you need to interrupt the recording to perform another task, click Pause Recording. When you are ready to resume recording, click Pause Recording again.

  4. When you are finished recording the macro, click Stop Recording.

    The Recording Complete dialog box appears.

  5. Name the macro and choose the location where you want to save it.

  6. If you want to run the macro every time the session connects, select Make this the Connect Macro.

    note

    You can fine-tune recorded macros by using the Visual Basic Editor. For more information, see Editing a Macro.

  7. If your macro doesn't play back like you expect, delete the macro and follow best practices to carefully record the macro again.

To delete a macro

  1. Open the Run Macro dialog box. The steps depend on your user interface mode.

    User Interface Mode Steps
    Ribbon On the Session tab, from the Macros group, click Run Macro.
    Classic and Classic MDI On the Macro menu, choose Macros.
  2. Select Reflection Workspace Macro.

  3. In the Macros dialog box, select the macro you want to remove and then click Delete.

Creating a Macro in the Visual Basic Editor

Whenever possible, create macros in Modules. Doing so will add to the integrity and stability of your program. The exception to this rule is event procedures, which are added directly to Reflection objects.

To create a macro in the Visual Basic Editor

  1. In Reflection, open a session document.

  2. Open the Visual Basic Editor. The steps depend on your user interface mode.

    User Interface Mode Steps
    Reflection Ribbon From the Macros tab, click Visual Basic.
    Reflection Browser On the Reflection menu, choose Tools, Macro and then Visual Basic.
    TouchUx Tap the Wrench icon and then under Macro, select Visual Basic.

    The session document appears as a project in the Project Explorer ; for example, Project ( My Session.rd3x).

  3. In the Project Explorer, select the project in which you want save the macro, and then choose Insert > Module to create a new module.

  4. Double-click the module you created.

    The Code window opens.

  5. Choose Insert > Procedure, type a name in the Name box, and then click OK.

    The name you choose must follow the Visual Basic naming conventions for macros. For further information, see Naming Macros.

  6. Type code for your macro between the Sub (or Public Sub) and End Sub statements.

    Context-sensitive Help is available for Visual Basic commands. Position the insertion point within a command and press F1.

Editing a Macro

Use the Visual Basic Editor to edit Reflection macros.

To edit a macro

  1. Open the Visual Basic Editor. The steps depend on your user interface mode.

    User Interface Mode Steps
    Reflection Ribbon From the Macros tab, click Visual Basic.
    Reflection Browser On the Reflection menu, choose Tools, Macro and then Visual Basic.
    TouchUx Tap the Wrench icon and then under Macro, select Visual Basic.

    The Macros dialog box appears.

  2. In the Macro name box, select the name of the macro to edit and click Edit.

  3. In the Visual Basic Editor, type in or edit the macro commands.

  4. From the File menu, choose Save.

Run a Startup Macro

You can set up a Visual Basic for Applications (VBA) macro to run when a Reflection workspace starts, rather than when a session opens and connects to the host.

This allows you to gather information about how users will connect and then use that information to configure session settings.

For example, you can create a startup macro to perform tasks such as:

  • displaying a VBA UserForm to gather information from the user before connecting to the host

  • reading from an .ini file

  • checking for host or router availability

  • configuring Reflection settings

caution

You can configure only macros in the Common project to run when the workspace starts. Do not configure the "Run Reflection Workspace Macro" action to run a macro present in a session document ( rd0x, rd3x, rd5x). This prevents Reflection from starting properly.

To set up a startup macro

  1. Open the Reflection Workspace Settings dialog box. The steps depend on your user interface mode.

    User Interface Mode Steps
    Ribbon On the File menu or the Reflection button (if using the Office 2007 Look and Feel), choose Reflection Workspace Settings.
    Reflection Browser On the Reflection menu, choose Settings and then Reflection Workspace Settings.
    TouchUx Tap the Gear icon and then select Reflection Workspace Settings.
  2. Under Workspace Settings, click Configure Workspace Settings.

  3. Under Workspace and Documents, in the When starting workspace list, select Run Startup action.

  4. Click Select Action.

  5. Under Action, select Run Reflection Workspace Macro.

  6. Under Action parameters, choose Select macro.

  7. In the Select a macro box, select the macro you want to run when Reflection starts.

Set up Macros that Run Before or After a Host Connection

If you have created a macro for your session or workspace, you can set up the session to run a connection macro.

To set up a connection macro

  1. Open the Settings window. The steps depend on your user interface mode.

    User Interface Mode Steps
    Ribbon or Reflection Browser With a session open in Reflection, from the Quick Access Toolbar, click .
    TouchUx Tap the Gear icon and then select Document Settings.
  2. On the Settings dialog box, do one of the following:

    • (VT)click Configure Connection Settings.

    • (3270 or 5250), click Configure Advanced Connection Settings.

  3. Under Connection Action, select whether to run the macro before or after the initial connection.

  4. Click Select Action and select the macro to run.

Naming Macros

Observe the following rules when you name Visual Basic macros (including procedures, constants, variables, and arguments):

  • Use a letter as the first character. (Names aren't case sensitive, but they preserve capitalization.)

  • Use only alphanumeric characters and the underscore character ( _ ). Spaces and other symbols are not allowed.

  • Use fewer than 255 characters.

  • Avoid names that match Visual Basic or Reflection commands. Or, if you do use a macro name that is the same as a command, fully qualify the command when you want to use it. (To do this, you need to precede the command name with the name of the associated type library. For example, if you had a macro named Beep, you could only invoke the Visual Basic Beep statement using VBA. Beep.)

  • Give unique names to macros within a single module. Visual Basic doesn't allow you to have two macros with the same name in the same code module. However, you can have two macros with the same name if they are in different code modules. For example, although you could not have two macros named StartUp in the same code module, you could have two macros named StartUp if they were in different code modules. To call a macro with a duplicate name that is in another code module, you must fully qualify the macro name. (For example, Module1.StartUp invokes the StartUp macro in Module1.)