Save Screens to Word as Images

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

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
            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
            '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     
          IbmScreen_BeforeSendControlKey = True    
    End Function                      
  2. Save the session and then reopen it.
  3. Enter some data in a terminal session screen and open the log.docx Word file to check the results.


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