Reflection Desktop VBA Guide
Quickstart / Record and Edit a Macro
In This Topic
    Record and Edit a Macro
    In This Topic

    You can record macros to automate the following types of actions:

    You cannot record:

    Recording a macro is an easy way to get started on a more complex macro. After you save a recorded macro, you can edit it in the Visual Basic editor to add functionality and make it easier to maintain.

    If you are working with an unstructured display format (such as Open Systems terminals), you'll probably find it useful to record a macro and then look at the recorded code to get information such as the strings returned by a host or the screen positions of selected data.

    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.

    • If your macro is not working as expected after recording it again, manually edit the macro in the VBA editor to remove duplicate blocks of code that were created while recording. (Do not attempt to manually remove code blocks unless you have a good knowledge of the VBA programming language and syntax, as well as knowledge of the host screens being navigated.)

    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.

    Record and modify a macro

    This example shows how to record a macro that copies data to the clipboard. It also shows how to modify the recorded code to disconnect the session after the data is copied.

    This article contains tabbed content that is specific to each terminal type. Be sure the tab for your Which Terminal Type are you Using? is selected.
    • Select Record Macro.
    • Perform the task(s) that you want to automate.
    • (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. When you are finished recording the macro, click Stop Recording.
    • The Recording Complete dialog box appears.
    • Name the macro, choose the location where you want to save it, and then click OK.

    Record and test a macro

    1. In a Reflection IBM terminal session, on the Tools tab, select Record Macro.
    2. Select some text on the screen.
    3. Right click on the selected text and then select Copy.
    4. Select Stop Recording and then save the recorded macro in the current document's project.
    5. Select and copy some other data so you can test the macro.
    6. Select Run Macro and then run the new macro you just recorded.
    7. Open a Word document and paste the data copied by the macro into the document.  

    Modify the macro in the VBA Editor

    1. On the Tools tab, select Visual Basic.
    2. In the Visual Basic Editor Project Explorer window, open the Project folder and then in the Modules folder, select Recorded and then select the module that has the name of the macro. The recorded macro code is displayed in the code window.
      Recorded macro code to declare variables
      Copy Code
      Sub CopyData()
      '---------------------------------------------------------------------
      ' Generated by Attachmate Reflection 2014 (15.6.660.0)
      ' Generated by the Macro Recorder on 7/7/2014 5:13:44 PM
      '---------------------------------------------------------------------
          'Common variable declarations
          Dim ibmCurrentTerminal As IbmTerminal
          Dim ibmCurrentScreen As IbmScreen
          Dim hiddenTextEntry As String
          Dim returnValue As Integer
          Dim timeout As Integer
          timeout = 15000
          Set ibmCurrentTerminal = ThisFrame.SelectedView.control
          Set ibmCurrentScreen = ibmCurrentTerminal.screen
                                         
          ibmCurrentScreen.SetSelectionStartPos 1, 24
          ibmCurrentScreen.ExtendSelection 14, 44
          ibmCurrentScreen.Copy
       End Sub
      
    3. To modify the macro to automate other actions that not supported by the macro recorder, use the terminal and screen variables created by the macro. For example, to disconnect the session after the data is copied, you can add the following line at the end of the macro:
      Disconnect the session
      Copy Code
      ...
          ibmCurrentTerminal.Disconnect
      End Sub
      
    • Select Record Macro.
    • Perform the task(s) that you want to automate.
    • (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. When you are finished recording the macro, click Stop Recording.
    • The Recording Complete dialog box appears.
    • Name the macro, choose the location where you want to save it, and then click OK.

    Record and test a macro

    1. In a Reflection VT terminal session, on the Tools tab, select Record Macro.
    2. Select and copy some data on the screen.
    3. Select Stop Recording and then save the recorded macro in the current document's project.
    4. Copy some other data to the clipboard so you can test the macro.
    5. Select Run Macro and then run the new macro you just recorded.
    6. Open a Word document and paste the data into the document.

    Modify the macro in the VBA Editor

    1. On the Tools tab, select Visual Basic.
    2. In the Visual Basic Editor Project Explorer window, open the Project folder and then in the Modules folder, select Recorded and then select the module that has the name of the macro.
      The VBA code for the recorded macro is displayed in the code window.
      Recorded Macro Code
      Copy Code
      Sub CopyData()
          Call ExternalRecorded1
      End Sub
      
      Sub ExternalRecorded1()
      ' Generated by the Reflection Macro Recorder on 07-08-2014 12:00:37.96.
      ' Generated by Attachmate Reflection 2014 (15.6.660.0).
          Dim osCurrentScreen As Screen
          Dim osCurrentTerminal As Terminal
          Dim returnValue As Integer
          Set osCurrentTerminal = ThisFrame.SelectedView.control
          Set osCurrentScreen = osCurrentTerminal.Screen   
          'Select and copy text from the screen
          osCurrentScreen.SelectText 2, 22, 19, 55, RegionOption_Wrapped
          'Press EditCopy (Copy the selection and put it on the Clipboard).
          osCurrentScreen.Copy
          Exit Sub
          'Recording stopped at 12:00:51.92.
      End Sub 
      

    3. To modify the macro to automate other actions not supported by the macro recorder, use the terminal and screen variables created by the macro. For example, to disconnect the session after the data is copied, you can add the following line at the end of the macro:

      Disconnect the session
      Copy Code
      ...
         osCurrentTerminal.Disconnect
      End Sub
      

    Run the macro from the VBA Editor

    To run the macro from the editor, place the cursor in the code window and press F5.

    Concepts

    When you record a macro, the macro recorder creates variables for two important objects (the names of the objects are unique to the terminal type):

    The macro uses the ThisFrame property to get the control for the session that is selected (displayed in the workspace) during the recording. ThisFrame is a property that is available in any Reflection macro. It returns the top-level user interface component for the workspace and can be used to access the controls (sessions) running in the workspace.

    For more about these objects, see Using the Reflection Object Model.

    See Also