Reflection Desktop VBA Guide
Key Concepts / Using Reflection Events
In This Topic
    Using Reflection Events
    In This Topic

    Many of the Reflection objects support automation events. Most of these objects appear in the Visual Basic Editor project pane (for example, IbmScreen or ThisScreen).

    Key Reflection events occur when:

    You can use Reflection events in Reflection macros or in Macros created with other applications. You can also define custom events.

    Getting Started With Reflection Events

    Using Reflection Events in Sessions Created At Runtime

    Defining Custom Events

    Getting Started With Reflection Events

    If you are developing a macro in Reflection, you can use the Visual Basic editor to set up events for a session project.  

    In the project window, open a code view for one of the default  Reflection objects. The top of the code window has has two list boxes with default settings of (General) and (Declarations).


    When you change the (General) setting to the object you selected
    (for our example, we selected ThisView), an event procedure is inserted into the Code window automatically.

    You can select other events from the list box on the right. For our example, we'll choose Closing.

    In the event procedure you just created for the Closing event, enter the code you want to execute when the event occurs and then save this procedure as part of your session document. The following sample uses the View_Closing event to display a dialog box that requires the user to confirm closing:  

    Using the Closing event
    Copy Code
    Private Function View_Closing(ByVal sender As Variant) As Boolean
      If MsgBox("Confirm Close", vbOKCancel) = vbOK Then
        Debug.Print "True"
        View_Closing = True
      Else
        Debug.Print "False"
        View_Closing = False
      End If
    End Function
    

    Using Reflection Events in Sessions Created at Runtime

    You may need to write macros that start other sessions at runtime. For example, you may need to open a session from an Excel macro or you might need to start a session from a Reflection macro if a specific option is selected by a user. To use events for Reflection sessions started at runtime, you'll need to set up your macro to handle the events. 

    First, make sure the VBA Project for your Reflection session (or Excel worksheet) has the references for the library you need. If you are in an IBM module and you are adding an Open Systems terminal (or vice versa), you'll need to select the project folder and then add a reference to the library for the terminal you are adding to the project folder. 

    You don't need to add references for Web objects in Reflection macros, because these objects are part of the Reflection Objects library, which is referenced for all sessions.

    In our example, we are creating an open systems terminal in an IBM session macro, so we'll need to add a reference to the Reflection library required for Open Systems objects. (The References dialog box is opened from the Tools | References menu.)

    Then set up your macro to handle events for the Reflection objects. The default Reflection objects in the Visual Basic editor (such as ThisIbmScreen or ThisScreen) are not available for the new session while you are programming, so you'll need to set up variables for these objects using the VBA WithEvents keyword. The easiest way to declare these variables is to place them in the object module in which you are writing the macro (For example, ThisIbmScreen or ThisScreen for Reflection or ThisWorkbook or Sheet for Excel).

    You can also declare these variables in a custom class module but  you cannot declare them in a regular code module. 

    In our example, we declare a public global variable for an Open Systems terminal object in the code window of the ThisIbmTerminal Reflection Object. 

    Declaring global variable with events in a default class module
    Copy Code
    'Declare this variable in a default object code window
    Public WithEvents osTerminal As Attachmate_Reflection_Objects_Emulation_OpenSystems.terminal
    

      

    When you select the osTerminal on the left selection box, you have access to the events for the new Open Systems control.

     

    After the events are available in the VBA editor, you can select these events and handle them.

    Handling events
    Copy Code
    Private Function osScreen_ControlKeySending(ByVal sender As Variant, Key As Attachmate_Reflection_Objects_Emulation_OpenSystems.ControlKeyCode) As Boolean
      'Insert code to handle event here
    End Function
    

    Defining Events

    Using the Reflection DefineEvent method, you can define events that occur when:

    This method is a member of the Screen object. You can use it to define an event in a code module procedure or in another event procedure. 

    This sample defines an event that occurs when there is no host activity for a specified interval of time.

    Defining an event
    Copy Code
    Sub DefineTimeout()
        'Define an event that occurs when there is no host activity for 30 seconds
        ThisIbmScreen.defineEvent 1, DefinedEventType_Silence, "00:00:30", " ", 0, 0
    End Sub
                            
    
    Defining an event
    Copy Code
    Sub DefineTimeout()
         'Define an event that occurs when there is no host activity for 30 seconds
         ThisScreen.defineEvent 1, DefinedEventType_Silence, "00:00:30", " ", 0, 0
     End Sub
    

     

    After you have defined the event, you will need to create an event procedure to handle the event. You'll need to use the event number to identify the event (in our sample, we set the event number as 1)

    This procedure disconnects from the terminal when the defined event occurs.

    You'll need to add this event procedure to the ThisIbmScreen object code window for your session.

    Adding a defined event
    Copy Code
    'The following procedure is executed when a defined event triggers.
    Private Sub IbmScreen_DefinedEvent(ByVal EventNumber As Long, ByVal EventType As Attachmate_Reflection_Objects_Emulation_IbmHosts.DefinedEventType, ByVal TheString As String,  _
         ByVal row As Long, ByVal column As Long)
        'If the host is inactive for the specified interval, disconnect
        If EventNumber = 1 Then
            ThisIbmTerminal.Disconnect
        End If
    End Sub
                           
    

    You'll need to add this event procedure to the ThisScreen object code window for your session.

                   
    Adding a defined event
    Copy Code
    'The following procedure is executed when a defined event triggers.
    Private Sub Screen_DefinedEvent(ByVal EventNumber As Long, ByVal EventType As Attachmate_Reflection_Objects_Emulation_OpenSystems.DefinedEventType, ByVal TheString As String, ByVal row As Long, ByVal column As Long)
         'If the host is inactive for the specified interval, disconnect
         If EventNumber = 1 Then
             ThisTerminal.Disconnect
         End If
     End Sub
    End Sub
    

    Events defined in this way remain defined as long as your Reflection session lasts, and for many events, this is a good solution. But this event is handled to disconnect the session. If you define it in a session module, it will be undefined after it disconnects the session. Since this is a timeout event, it makes since to redefine it when we reconnect. Instead of defining it in a session module, you can define the event in another event, such as the Terminal Object Connect event.

    Defining an event in another event
    Copy Code
    Private Sub IbmTerminal_AfterConnect(ByVal sender As Variant)
             'Define an event that occurs when there is no host activity for 30 seconds
          ThisIbmScreen.defineEvent 1, DefinedEventType_Silence, "00:00:30", " ", 0, 0
    End Sub
    
    Defining an event in another event
    Copy Code
    Private Sub Terminal_Connected(ByVal sender As Variant, ByVal ConnectionID As Long, ByVal connnectionType As  Attachmate_Reflection_Objects_Emulation_OpenSystems.ConnectionTypeOption, ByVal connectionSettings As Variant)
             'Define an event that occurs when there is no host activity for 30 seconds
          ThisScreen.defineEvent 1, DefinedEventType_Silence, "00:00:30", " ", 0, 0
    End Sub
    

     

    You can also remove events during your session. If you want to define the event for part of your macro and then remove it, you can use the Remove method. 

    Removing a defined event
    Copy Code
    'This procedure removes event number 1
    Sub ClearEvent1()
       ThisIbmTerminal.RemoveEvent (1)
    End Sub
    
    'This procedure removes all defined events
    Sub ClearEvent1()
       ThisIbmTerminal.ClearEvents
    End Sub
    
    Removing a defined event
    Copy Code
    'This procedure removes event number 1
    Sub RemoveEvent1()
       ThisTerminal.RemoveEvent (1)
    End Sub
    
    'This procedure removes all defined events
    Sub RemoveAllEvents
       ThisTerminal.ClearEvents
    End Sub
    
    See Also