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

    You can create 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 create a Reflection session 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 create-ibm-session-from-excel.xlsm (IBM) and create-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 Reflection from an Excel Macro

    1. In a Microsoft Excel worksheet, open the Visual Basic Editor and insert a new code module. 
    2. 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 create an IBM session)
      • Attachmate_Reflection_Objects_Emulation_OpenSystems (if you want to create an Open Systems session)
      • Attachmate_Reflection_Objects_Framework
                       
    3. Copy the following code into your new code module.
      Open a Reflection Session from Excel
      Copy Code
      Private Sub CreateReflectionIBMSession()
          'Declare an object variable for the Reflection application object:
          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 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 initalized
              Do While .IsInitialized = False
                  .Wait 200
              Loop
             
              'Get a handle to the frame
              Set frame = .GetObject("Frame")
             
              'Make the frame visible so we can view the workspace
              frame.Visible = True
          End With
         
          'Create an Ibm3270 control and set the host address
          Set terminal = app.CreateControl2("{09E5A1B4-0BA6-4546-A27D-FE4762B7ACE1}")
          terminal.HostAddress = "demo:ibm3270.sim"
          terminal.Port = "623"
         
          'For a 5250 ibmterminal control, use the following Guid:
          'Set terminal = app.CreateControl2("{AF03A446-F278-4624-B9EF-C896DF2CA1CA}" )
      
          'Create a view to display the session
          Set view = frame.CreateView(terminal)
      End Sub
      
      Open a Reflection Session from Excel
      Copy Code
      Private Sub CreateReflectionOpenSystemsSession()
          'Declare an object variable for the Reflection object:
          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 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 a terminal control and set the host address
          Set terminal = app.CreateControl2("{BE835A80-CAB2-40d2-AFC0-6848E486BF58}")
         
          'Create a Telnet connection and set the host address
          terminal.ConnectionSettingsTelnet.HostAddress = "yourHostName"
      
          'Create a view so that we can display the session
          Set view = frame.CreateView(terminal)
      
      End Sub
      
                                     
    4. If you are using the Open Systems sample, change the placeholder on the line of code that specifies the HostAddress to your host name or IP address.
    5. Place the cursor in the code window and then press F5 to run the macro.

    Concepts

    To create a session from another application, you'll need to use the Reflection ApplicationObject, Frame, View, and terminal objects.

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

    This sample uses the CreateControl2 method to create the terminal control for the session. This method returns the appropriate terminal control for the GUID value that you pass to it. Each type of terminal supported by Reflection has a unique GUID, as shown below:

    Terminal Control GUID
    Ibm 3270 {09E5A1B4-0BA6-4546-A27D-FE4762B7ACE1}
    Ibm 5250 {AF03A446-F278-4624-B9EF-C896DF2CA1CA} 
    VT (Open Systems) {BE835A80-CAB2-40d2-AFC0-6848E486BF58}
    Web {F1F058B1-0472-4095-A782-3D7333813AD0}

     

    The terminal control is used to set the host address. For IBM sessions, you can also use it to set the port.

    terminal.HostAddress = "demo:ibm3270.sim"
    terminal.Port = "623"
    


    For Open Systems sessions, you will need to set the type of connection you want to use and then specify the host address. Reflection supports a number of connection types, including Telnet and SSH.

    'Create a Telnet connection and set the host address
    terminal.ConnectionSettingsTelnet.HostAddress = "yourHostName"
    


     

    See Also