How to use the VBA View_Closing() method to perform an action when closing a host session

  • 7023935
  • 12-Jun-2019
  • 08-Aug-2019

Environment

Reflection Desktop (including Pro, for X, for IBM, or for UNIX and OpenVMS) 16.2 or higher

Situation

This article describes how to use the VBA View_Closing() method to perform an action when closing a host session.  The View_Closing method can be used to capture the end-user manually attempting to close the host session, in a variety of ways, and then allow the VBA program to take some specific action, such as preventing the user from closing the host session without logging out of the host application, or perform some background operation automatically at host session close time.

Resolution

The following VBA macro will allow the user to run and save host session configuration changes in their My Documents location, but also allow the user to load and run host session files from a READ ONLY location, where they will not be able to save host session configuration changes.  This VBA code and solution requires the Shared Macro support included with Reflection Desktop 16.2 and higher and also requires two specific configuration settings:

            1. Deploy Reflection Desktop 16.2 with the Workspace setting to “Discard document settings†when “When closing a document†in the application.settings file.

            2. Deploy a SharedMacros.RD3X file (with the VBA code below) to the users My Documents location where the users normally would save their RD3X files.
                This SharedMacros.RD3X file will contain VBA code to automatically save the host session file when the user closes the Host session.
                Starting with Reflection Desktop 16.2, the SharedMacros.RD3X file will “share†its macro code with all other RD3X files in the same folder.

To create the SharedMacros.RD3X file:

1. Open the Reflection Workspace

2. Set the Reflection Workspace Settings / Configure Workspace Defaults / “When closing a document†to “Discard document settingsâ€

3. Close the Reflection Workspace

4. Re-open the Reflection Workspace

5. Create a new 3270 Terminal session

6. When prompted for Connection information, just press OK

7. Let the session timeout the connection and then press Cancel in the “Enter Host Name or IP Addressâ€

8. From the File menu perform a SaveAs and save the file under the name of “SharedMacros.RD3Xâ€

9. In this Host session document, open the Macros / Visual Basic Editor

10. In the left panel, under the SharedMacros(SharedMacros.rd3x) entry, use the plus symbol to expand the section under the Reflection Objects

11. Locate the “ThisView†and double-click on it

12. Insert the following VBA code in the right panel that appears:

    Private Function View_Closing(ByVal sender As Variant) As Boolean
        ' capture the event of the user closing the Host session and decide what to do - save or discard the settings file
        ' if running from "C:\Program Files (x86)\Micro Focus\Reflection" don't save
        ' if running from "C:\Users\<username>\Micro Focus\Reflection" auto save
        Dim FullRD3XName As String
        Dim FPath As String
               
        ' get the fully qualified path name and RD3X session file which is currently running
        FullRD3XName = ThisIbmTerminal.SessionFilePath
   
        ' pull the path/folder name from the fully qualified session name string
        ' search backward from the end of the string to find the first "\" character from the end of string
        FPath = Left(FullRD3XName, InStrRev(FullRD3XName, "\") - 1)
   
        ' check the path/folder name where the RD3X session file was launched from for WRITE Permission
        If (IsPathWritable(FPath)) Then
            ' Save the host session under the existing file name
            ThisIbmTerminal.SaveAs (FullRD3XName)
        End If
        View_Closing = True ' allow session to close
    End Function

    Function IsPathWritable(ByVal FPath As String) As Boolean
        Dim FName As String
        Dim FHdl As Integer
        Dim Counter As Integer
        ' add a backslash to the end of the folder name
        If (Right(FPath, 1) <> "\") Then FPath = FPath & "\"
            ' create a temporary file name
            Do
                FName = FPath & "TempFile" & Counter & ".tmp"
            Loop Until Dir(FName) = ""
        ' if folder is Read Only then exit
        On Error GoTo CantWrite
        ' check to see if a temporary file can be created
        FHdl = FreeFile()
        Open FName For Output Access Write As FHdl
        Print #FHdl, "TESTWRITE"
        Close FHdl
        ' file can be created so folder is writeable
        IsPathWritable = True
        Kill FName
        Exit Function
    CantWrite:
        IsPathWritable = False
    End Function

13. Save this RD3X file as SharedMacros.RD3X.  This is a required name.

14. Place the SharedMacros.RD3X file in the users My Documents location where the other RD3X files will reside.
      C:\Users\<username>\Documents\Micro Focus\Reflection

Now with this View_Closing macro in place, when the user closes the host session (in any manner) the macro will kick off and detect if the RD3X file can be saved.  If the macro is in a folder where the user has WRITE permissions, then the macro will do this save automatically.  No prompts to save the macro will occur.