Generate Email

You can automate Outlook or other email programs to automatically generate emails that include data or text displayed on the screen.

This sample shows how to create an email that uses screen data for it's body text, gets text from the screen and adds it to the email's subject line, and then adds an email address.

This article contains tabbed content that is specific to each terminal type. Be sure the tab for your terminal type is selected.

To run this sample

  1. Create a new Ibm 3270 terminal session and enter "demo:ibm3270.sim" in the Host name / IP Address field.
  2. On the first screen, enter any credentials.
  3. On the second screen, enter "kayak".
  4. In the Visual Basic Editor Project window, under the session project folder, create a new module, copy the code into the new module, and press F5 to run the macro.
    Email screen data
    Copy Code
    Sub CreateEmail()
        Dim myOutlookApp As Object
        Dim mail As Object
        Dim screenID As String
        Const ERR_APP_NOTRUNNING As Long = 429
        'Get text to identify screen
        screenID = ThisIbmScreen.GetText(1, 25, 31)
       
        'Don't run the macro unless the program is on the screen with the data
        If screenID = "INTERNATIONAL KAYAK ENTERPRISES" Then
            ' Get a handle to the Microsoft outlook object
            On Error Resume Next
            Set myOutlookApp = GetObject(, "Outlook.Application")
            'If Outlook is not open, create an instance
            If Err = ERR_APP_NOTRUNNING Then
                Set myOutlookApp = CreateObject("Outlook.Application")
            End If
          
            'Create an email message and pass in a null string to include all the screen text in the body
            ThisIbmTerminal.Productivity.OfficeTools.CreateNewEmailMessage (vbNullString)
          
            'Get the new email object
            Set mail = myOutlookApp.ActiveInspector.CurrentItem
          
            'Use the HTML format
            mail.olBodyFormat = olFormatHTML
           
            'Get text from the screen for the subject
            mail.Subject = ThisIbmScreen.GetText(3, 31, 20)
          
            'Use a text string for the email address
            mail.Recipients.Add ("salesGroup@xyz.com")
              
            'uncomment to send the email automatically
            'mail.send
          
            Set myOutlookApp = Nothing
            Set mail = Nothing
          
        Else:
            MsgBox ("You must be on the 'INTERNATIONAL KAYAK ENTERPRISES' screen to send a sales status email.")
        End If
    End Sub                      
    
                   

To run this sample

  1. Create a new VT terminal session and enter "demo:UNIX" in the Host name / IP Address field.
  2. On the first screen, enter any credentials.
  3. On the second screen, enter "demodata". 
  4. In the Visual Basic Editor Project window, under the session project folder, create a new module, copy the code into the new module, and press F5 to run the macro.
    Email screen data
    Copy Code
    Sub CreateEmail()
        Dim myOutlookApp As Object
        Dim mail As Object
        Dim screenID As String
        Const ERR_APP_NOTRUNNING As Long = 429
       
        'Get text to identify screen
        screenID = ThisScreen.GetText2(2, 28, 2, 52)
                                   
        If screenID = "XYZ Company Monthly Sales" Then
       
            'Get a handle to the Microsoft outlook object
            On Error Resume Next
            Set myOutlookApp = GetObject(, "Outlook.Application")
           
            'If Outlook is not open, create an instance
            If Err = ERR_APP_NOTRUNNING Then
                Set myOutlookApp = CreateObject("Outlook.Application")
            End If
          
            'Create an email message and pass in a null string to include all the screen text in the body
            ThisTerminal.Productivity.OfficeTools.CreateNewEmailMessage (vbNullString)
          
            'Get the new email object
            Set mail = myOutlookApp.ActiveInspector.CurrentItem
          
            mail.olBodyFormat = olFormatHTML
           
            'Use text from the screen for the subject
            mail.Subject = ThisScreen.GetText2(2, 28, 2, 52)
          
            'Use a text string for the email address
            mail.Recipients.Add ("salesGroup@xyz.com")
                   
            'Uncomment to automatically send email
            'mail.send
          
            Set myOutlookApp = Nothing
            Set mail = Nothing
          
        Else:
            MsgBox ("You must be on the 'XYZ Company Monthly Sales' screen to send a sales status email.")
        End If
    End Sub
    

Concepts

This macro uses a screen recognition to ensure the program is on the right screen. When the macro is invoked on the screen that has screen data, it gets or creates an Outlook application object.

One way to fully automate this task would be to navigate to the required screen and then run this macro (see Navigating Sessions). 

Then it uses the InfoConnect CreateNewEmailMessage method to create an email message. Passing in a null value captures the entire screen and places it in the body. (Another option is to pass a region of the screen to this method captured using an InfoConnect method such as the Screen object's GetText3 method.)   

After you have an email object, you can set the format, subject, recipients, and other properties just as you would if you were writing an Outlook VBA macro. You can get text from the string as we did for the subject or add your own strings as we did for the email recipient.

 

See Also