Reflection Desktop VBA Guide
Key Concepts / Controlling Macro Execution
In This Topic
    Controlling Macro Execution
    In This Topic

    There are a number of ways to execute macros:

    Setting up Keyboard Maps or Buttons for Macros

    Running Macros When Certain Events Occur

    Running Macros When Certain Screens Are Recognized

    Running Reflection Macros from Excel or Another Application

    Running a Macro in Another Session Document

    Running a Macro in the Common Project

    Setting up Keyboard Shortcuts or Buttons for Macros

    You can set up macros to run from keyboard shortcuts or from buttons on the interface.

    Setting up Keyboard Shortcuts to Run Macros

    You can add a keyboard shortcut that runs a macro by modifying the keyboard map for your session. You can do this in two ways: 

    Setting up Buttons to Run Macros

    You can add buttons to the Reflection Ribbon that run macros (see "Add a Button to Run a Macro" in the Reflection Help.)

    You can also display the button or make it active only on certain screens or for certain users (see  Dynamically Change the User Interface.)

    Note: If you add a button, you will need to deploy a custom ribbon (ribbon.xuml) file with the session. 

     

    Running Macros When Certain Events Occur

    You can trigger macros with events. Key Reflection events occur when:

     This sample runs a macro before the session closes.

    Running a macro when the mouse is clicked on the screen
    Copy Code
    Sub TurnOnScratchPad()
      'Toggle the Scratchpad on or off
      ThisIbmTerminal.Productivity.ScratchPadPanelVisible = True
    End Sub
    Private Sub IbmScreen_MouseClick(ByVal windowMessage As Long, ByVal x As Long, ByVal y As Long)
      'Run a macro that toggles the Scratchpad on or off
      TurnOnScratchPad
    End Sub
    
    Running a macro when the mouse is clicked on the screen
    Copy Code
    Sub Demo()
      Debug.Print "Run this Macro when the mouse is clicked"
      Debug.Print "Row: " & ThisScreen.MouseRow
      Debug.Print "Column: " & ThisScreen.MouseColumn
    End Sub
    
    Private Sub Screen_MouseClick(ByVal windowMessage As Long, ByVal x As Long, ByVal y As Long)
      'Run a Macro
      Demo
    End Sub
    

    Running macros When Certain Screens or Strings Are Recognized

    You can automatically run macros when certain screens are displayed.

    This sample runs a macro after the second screen of the Reflection IBM3270 demo is recognized. It gets the text in a specific location after each screen is ready and compares it to the text found on this location on the second screen.

    Running a macro when a screen is recognized
    Copy Code
    Sub DemoMacro()
      Debug.Print "Run this macro on the second screen of the IBM demo"
    End Sub
    
    Private Sub IbmScreen_NewScreenReady(ByVal sender As Variant)
      Dim ScreenID1 As String
      Dim Rtn As ReturnCode
       
      'On the second screen of the demo, the text in this location is ATM5
      ScreenID1 = ThisIbmScreen.GetText(1, 7, 4)
          
      If ScreenID1 = "ATM5" Then
        'run the macro
        DemoMacro      
      End If
    End Sub
    

    This sample runs a macro after the tabular data on the Reflection UNIX demo is displayed. It uses the WaitForString method to identify the prompt for the data. (The string used to identify the prompt was obtained from a recorded macro.)

    Running a macro after a returned string is recognized
    Copy Code
    Sub DemoMacro()
       Debug.Print "Run this Macro after the data is displayed"
    End Sub
    
    Private Sub Screen_ControlKeySent(ByVal sender As Variant, ByVal Key As Attachmate_Reflection_Objects_Emulation_OpenSystems.ControlKeyCode)
      Dim returnValue As ReturnCode
      returnValue = ThisScreen.WaitForString3(LF & "Command> ", 1000,   WaitForOption.WaitForOption_AllowKeystrokes)
      If (returnValue = ReturnCode_Success) Then
        Debug.Print "Run Macro"
        DemoMacro
      End If
    End Sub
    

     

    Running Reflection Macros from Excel or Another Application

    You can run Reflection macros from Excel.

    This Excel macro runs the TurnOnScratchPad macro in a Reflection session. The session must be open and have focus in the Reflection workspace

    Running a Reflection macro from Excel
    Copy Code
                           
    Sub RunAMacro() 
      Dim app As Attachmate_Reflection_Objects_Framework.ApplicationObject
      Dim terminal As Attachmate_Reflection_Objects_Emulation_IbmHosts.IbmTerminal
      Dim view As Attachmate_Reflection_Objects.view
      Dim frame As Attachmate_Reflection_Objects.frame
          
      'Make sure a Reflection session that has the TurnOnScratchPad macro is running
      On Error Resume Next
      Set app = GetObject("Reflection Workspace")
      If Err.Number <> 0 Then 'no workspace
         MsgBox "Please open a Reflection session before running this"
         Exit Sub
      End If
          
      'Get handles to the frame, view, and terminal objects
      Set frame = app.GetObject("Frame")
      Set view = frame.SelectedView
      Set terminal = view.Control
                       
      'Run the macro in module1
      terminal.Macro.RunMacro2 MacroEnumerationOption_Document, "ThisIbmTerminal.TurnOnScratchPad"
     
    End Sub
    
    Running a Reflection macro from Excel
    Copy Code
    Sub RunAnOpenSystemsMacro()
      Dim app As Attachmate_Reflection_Objects_Framework.ApplicationObject
      Dim terminal As Attachmate_Reflection_Objects_Emulation_OpenSystems.terminal
      Dim view As Attachmate_Reflection_Objects.view
      Dim frame As Attachmate_Reflection_Objects.frame
                         
      'Make sure a Reflection session that has the TurnOnScratchPad macro is running
      On Error Resume Next
      Set app = GetObject("Reflection Workspace")
      If Err.Number <> 0 Then 'no workspace
            MsgBox "Please open a Reflection session before running this"
            Exit Sub
      End If
      'Get handles to the frame, view, and terminal objects
       Set frame = app.GetObject("Frame")
       Set view = frame.SelectedView
       Set terminal = view.Control
       'Run the macro in module1
       terminal.Macro.RunMacro2 0, "ThisTerminal.TurnOnScratchPad"
      
    End Sub
    

     

    Running a Macro in Another Session Document

    To access a macro in another session document VBA project, you can reference that project in the Visual Basic Editor. The advantage of a reference is that you can maintain a single copy of your macros in a centrally located project. If you make changes to a macro, all projects that reference it get the updated version. Changes you make to your referenced project have no effect on session documents that reference your project or to the macros they contain.

    To reference a project, follow the same process you use to add an object library reference.

    To run a referenced macro

    1. From the Visual Basic Editor, select Tools > References.
    2. From the Reference dialog box, find the opened project in the list box, select the check box for the referenced project, and then click OK.
    3. In the Visual Basic Editor, use one of the Reflection RunMacro methods to run the macro.

      This example runs a macro in the demoSession.rd3x session document. (This session must be open to run the macro.)
       
                                 
      Running a macro in another session
      Copy Code
      Sub RunAMacro()
          Dim terminal As IbmTerminal
          Dim viewObj As view
          'Get handles to the frame, view, and terminal objects
          'Set terminal = ThisFrame.SelectedView.titleText
         
          Set viewObj = ThisFrame.GetViewByTitleText("demoSession.rd3x")
          Set terminal = viewObj.control
                         
          'Run the macro in module1
          terminal.Macro.RunMacro2 MacroEnumerationOption_Document, "Module1.TurnOnScratchPad"
         
      End Sub
      
                                 
      Running a macro in another session
      Copy Code
      Sub RunAMacro()
          Dim Terminal As IbmTerminal
          Dim viewObj As View
          'Get handles to the frame, view, and terminal objects
          'Set terminal = ThisFrame.SelectedView.titleText
         
          Set viewObj = ThisFrame.GetViewByTitleText("demoSession.rd3x")
          Set Terminal = viewObj.control
                         
          'Run the macro in module1
          Terminal.Macro.RunMacro2 ProjectOption_CurrentProject, "Module1.TurnOnScratchPad"
         
      End Sub
      
            
      Note: To remove a reference, clear the check box for the referenced project in the Reference dialog box.

    Running a Macro in the Common Project

    You can create macros in the Reflection Common project and then run them from any session.

    To create a macro in the common project

    1. Open a Reflection session and then from the tools menu, select Visual Basic to open the Visual Basic Editor.
    2. Right-click on the Common project and select Insert > Module to insert a code module.
    3. Name the module ViewMacro.
    4. Enter the following code In the code module window and then select File > Save Common to save the macro.
      Macro in the Common project
      Copy Code
      Sub ChangeTitleOfSelectedView()    
          'Get the selected view and change its title
          ThisFrame.SelectedView.titleText = "Selected"  
      End Sub
      
    5. In the Visual Basic Editor Project folder for the session you opened, enter the following code in one of its code modules and then press F5 to run it.
      Macro that calls a macro in the Common project
      Copy Code
      Sub CallCommonMacro()
          Call ViewMacro.ChangeTitleOfSelectedView
      End Sub
      
    See Also