InfoConnect VBA Guide
HowTos / Save Screens to Word as Images
In This Topic
    Save Screens to Word as Images
    In This Topic

    This sample saves an image of a screen immediately before the screen data is transmitted and then appends the image to a Word file.

    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 save-screens-to-word.rd3x (IBM) file. The download package contains everything you need to run the macros in this file. See Download the VBA Sample Sessions.

    This sample applies only to IBM terminals

    To run this sample

    1. In an Ibm terminal session, open the Visual Basic Editor and then copy the code into the ThisIbmScreen code window.
      Save image of screen in a Word file
      Copy Code
       Sub PutImagesInWordDocument()
          Dim screenImage() As Byte
          Dim myWordApp As Object, myWordDoc As Object, ObjSelection As Object
          Dim fName As String, wfName As String
        
          'Error code returned by GetObject when application is not running
          Const ERR_APP_NOTRUNNING As Long = 429
        
          fName = Environ("USERPROFILE") & "\My Documents\ReflectionScreen.bmp"
          wfName = Environ("USERPROFILE") & "\My Documents\log.docx"
           
          'Get the current screen image and save it
          screenImage = ThisIbmTerminal.Productivity.ScreenHistory.GetLiveScreenImage()
          Open fName For Binary Access Write As #1
          Put #1, , screenImage
          Close #1
            
          On Error Resume Next
          'Get a handle to the Word Object
          Set myWordApp = GetObject(, "Word.Application")
        
          'If not open, create a word instance and document
          If Err = ERR_APP_NOTRUNNING Then
              Set myWordApp = CreateObject("Word.Application")
              Set myWordDoc = myWordApp.Documents.Add()
              'Write the user profile to the top of the file
              myWordApp.selection.TypeText Environ("USERPROFILE")
          
          'If open, get the document named log.docx file
          Else
              myWordDoc = Nothing
              'If log.docx is open, use it for the document
              For Each doc In myWordApp.Documents
                  If doc.Name = "log.docx" Then
                      Set myWordDoc = doc
                  End If
              Next
              'If not open, create a new document
              If myWordDoc Is Nothing Then
                  Set myWordDoc = myWordApp.Documents.Add()
                  'Write the user profile to the top of the file
                  myWordApp.selection.TypeText Environ("USERPROFILE")
              End If
            End If
          myWordApp.Visible = True
         
          'Put in text at the insertion point (selection)
          myWordApp.selection.TypeText "Screen closed at" & Now
        
          'Add the Screen image
          Set objShape = myWordApp.selection.InlineShapes.AddPicture(fName)
          'Save the word document with the wfName file name
          myWordDoc.SaveAs2 (wfName)
      End Sub
      
      Private Function IbmScreen_BeforeSendControlKey(ByVal sender As Variant, key As Long) As Boolean     
            PutImagesInWordDocument
            IbmScreen_BeforeSendControlKey = True    
      End Function                      
      
                             
    2. Save the session and then reopen it.
    3. Enter some data in a terminal session screen.
      Microsoft Word opens and displays the data. The data is displayed in the log.docx Word file in your user data folder.

    Concepts

    This sample handles the BeforeSendControlKey event to run the PutImagesInWordDocument Sub every time the program exits a screen.

    PutImagesInWordDocument  gets an image of the screen using the GetLiveScreenImage() method on the ScreenHistory object and saves it to a binary file. It assigns an instance of Microsoft Word to the myWordApp variable. Then it gets the log.docx document (if it is open) or opens it and assigns it to the myWordDoc variable. 

    Finally, it inserts a timestamp, the image of the screen in the binary file, and the user environment to the Word document and saves the document.

     

     

    See Also