Key Concepts > Controlling Macro Execution |
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 InfoConnect Macros from Excel or Another Application
Running a Macro in Another Session Document
Running a Macro in the Common Project
You can set up macros to run from keyboard shortcuts or from buttons on the interface.
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:
You can add buttons to the InfoConnect Ribbon that run macros (see "Add a Button to Run a Macro" in the InfoConnect 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. |
You can trigger macros with events. Key InfoConnect 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 |
You can automatically run macros when certain screens are displayed.
This sample runs a macro after the second screen of the InfoConnect 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 InfoConnect 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 |
You can run InfoConnect macros from Excel.
This Excel macro runs the TurnOnScratchPad macro in an InfoConnect session. The session must be open and have focus in the InfoConnect workspace
Running a InfoConnect 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 InfoConnect session that has the TurnOnScratchPad macro is running On Error Resume Next Set app = GetObject("InfoConnect Workspace") If Err.Number <> 0 Then 'no workspace MsgBox "Please open a InfoConnect 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 InfoConnect 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 InfoConnect session that has the TurnOnScratchPad macro is running On Error Resume Next Set app = GetObject("InfoConnect Workspace") If Err.Number <> 0 Then 'no workspace MsgBox "Please open a InfoConnect 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 |
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.
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. |
You can create macros in the InfoConnect Common project and then run them from any session.
Macro in the Common project |
Copy Code
|
---|---|
Sub ChangeTitleOfSelectedView() 'Get the selected view and change its title ThisFrame.SelectedView.titleText = "Selected" End Sub |
Macro that calls a macro in the Common project |
Copy Code
|
---|---|
Sub CallCommonMacro() Call ViewMacro.ChangeTitleOfSelectedView End Sub |