Reflection Desktop VBA Guide
Quickstart / Open a Session From a Microsoft Excel Macro
In This Topic
    Open a Session From a Microsoft Excel Macro
    In This Topic

    You can open a Reflection session from another application that supports VBA.

    Visual Basic uses a standard set of protocols called Automation (or OLE Automation) that allows one application to communicate with other applications. Any application that supports Automation can communicate with any other application. This means that a Reflection session can communicate with other Reflection sessions, Microsoft Office products, stand-alone Visual Basic, or any other product that supports Automation.

    This example shows how to open a Reflection session document from an Excel macro.

    If you prefer to run macros in pre-configured sessions instead of creating your own sessions, you can download the VBA Sample Sessions and open the open-ibm-session-from-excel.xlsm (IBM) and open-os-session-from-excel.xlsm (Open Systems) files. The download package contains everything you need to run the macros in these files. See Download the VBA Sample Sessions.

    Open a Reflection session from an Excel Macro

    1. In Reflection, create a session and save it as "mySavedSession" in your  ...\Documents\OpenText\Reflection\" folder.
    2. In a Microsoft Excel worksheet, open the Visual Basic Editor and insert a new code module.  
    3. On the Excel VBA Editor Tools menu, select References and then select the following Reflection Libraries:
      • Attachmate_Reflection_Objects
      • Attachmate_Reflection_Objects_Emulation_IbmHosts (if you want to open an IBM session)
      • Attachmate_Reflection_Objects_Emulation_OpenSystems (if you want to open an Open Systems session)
      • Attachmate_Reflection_Objects_Framework
    4. Copy the following code into the VBA Editor and press F5 to run it.
      Open a session
      Copy Code
      Private Sub OpenReflectionIBMSession()
          'Declare an object variable for the Reflection application
          Dim app As Attachmate_Reflection_Objects_Framework.ApplicationObject
          
          'Declare frame, terminal, and view object variables:
          Dim frame As Attachmate_Reflection_Objects.frame
          Dim terminal As Attachmate_Reflection_Objects_Emulation_IbmHosts.IbmTerminal
          Dim view As Attachmate_Reflection_Objects.view
       
          'If an instance of Reflection is open, get a handle to it
          On Error Resume Next
          Set app = GetObject("Reflection Workspace")
      
          'Otherwise, create a new instance of Reflection
          On Error GoTo 0
          If IsEmpty(app) Or (app Is Nothing) Then
             Set app = New Attachmate_Reflection_Objects_Framework.ApplicationObject
          End If
         
          With app
              'wait until Reflection initializes
              Do While .IsInitialized = False
                 .Wait 200
              Loop
             
             'Get a handle to the Frame object
              Set frame = .GetObject("Frame")
             
              'Make the frame visible so we can view the workspace
              frame.Visible = True
          End With
           
          'Create an Ibm3270 control using an .rd3x session document file
          Set terminal = app.CreateControl(Environ$("USERPROFILE") & _
          "\Documents\OpenText\Reflection\" & "mySavedSession.rd3x") 
      
          'Create a view so that we can display the session
          Set view = frame.CreateView(terminal)
      End Sub 
      
      Open a session
      Copy Code
      Private Sub OpenReflectionOpenSystemsSession()
          'Declare an object variable for the Reflection application
          Dim app As Attachmate_Reflection_Objects_Framework.ApplicationObject
         
          'Declare frame, terminal, and view object variables:
          Dim frame As Attachmate_Reflection_Objects.frame
          Dim terminal As Attachmate_Reflection_Objects_Emulation_OpenSystems.terminal
          Dim view As Attachmate_Reflection_Objects.view
             
          'If an instance of Reflection is already open, get a handle to it
          On Error Resume Next
          Set app = GetObject("Reflection Workspace")
      
          'Otherwise, create a new instance of Reflection
          On Error GoTo 0
          If IsEmpty(app) Or (app Is Nothing) Then
               Set app = New Attachmate_Reflection_Objects_Framework.ApplicationObject
          End If
        
          With app
              'wait until Reflection is initialized
              Do While .IsInitialized = False
                 .Wait 200
              Loop
              'Get a handle to the Frame object.
              Set frame = .GetObject("Frame")
            
              'Make the frame visible so we can view the workspace
              frame.Visible = True
          End With
        
          'Create an Open Systems terminal control using an .rdox sesssion document file
          Set terminal = app.CreateControl(Environ$("USERPROFILE") & _
          "\Documents\OpenText\Reflection\" & "mySavedSession.rdox")
            
          'Create a view so that we can display the session
          Set view = frame.CreateView(terminal)
      End Sub
      

    Concepts

    To open a session from another application, you'll need to use the Reflection ApplicationObject, IbmTerminal or Terminal, Frame, and View objects:

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

    See Also