Reflection Desktop VBA Guide
HowTos / Generate Email
In This Topic
Generate Email
In This Topic

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

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 generate-email.rd3x (IBM) and generate-email.rdox (Open Systems) files. The download package contains everything you need to run the macros in these files. See Download the VBA Sample Sessions.

 

This sample shows how to create an email that uses screen data for it's body text and adds an email address.

This article contains tabbed content that is specific to each terminal type. Be sure the tab for your Which Terminal Type are you Using? is selected.

This sample runs a macro that creates an email if the program is on the INTERNATIONAL KAYAK ENTERPRISES screen.

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                      
    
                   

Concepts

This macro gets or creates an Outlook application object.

Then it uses the Reflection 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 a Reflection 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.

 

See Also