InfoConnect for Unisys
Run VBA Macro

This sample opens a session document, prompts for a user log on, and then runs a demo macro in the document if the macro exists.

When the macro has completed execution, it closes the InfoConnect workspace.

 

To run a VBA macro

  1. Open the InfoConnect workspace and create a demo session as follows:

    If you are using IBM, create an IBM3270 session with a host name of "demo:ibm3270.sim" and save the session as demoSession.rd3x in the default folder (...\myDocuments\Micro Focus\InfoConnect.)

    If you are using Open Systems, create a VT session with a host name of "demo:UNIX" and save the session as demoSession.rdox in the default folder.

       
  2. Create a short macro in the VBA document project in module1, name it "DemoMacro", and then save and close the session.
  3. In Visual Studio, create a new Console Application project and add references for the following InfoConnect assemblies. (Depending on your version of Visual Studio, these can be found either on the .NET tab or under Assemblies | Extensions.)
    Attachmate.Reflection.Framework
    Attachmate.Reflection.Emulation.IbmHosts
    Attachmate.Reflection.Emulation.OpenSystems
    Attachmate.Reflection
                       
    Run a Macro
    Copy Code
    using System;
    using Attachmate.Reflection.Emulation.IbmHosts;
    using Attachmate.Reflection.Framework;
    using Attachmate.Reflection.UserInterface;
    namespace CreateSessionFromExistingSessionFile
    {
        public class Program
        {
            // Declare the InfoConnect application (workspace)
            private static Attachmate.Reflection.Framework.Application reflectionApplication;
            // Close InfoConnect after the macro has completed
            private static void rMacro_MacroCompleted(object sender, EventArgs e)
            {
                Console.WriteLine("Closing InfoConnect");
                reflectionApplication.Close(ApplicationCloseOption.CloseNoSave);
            }
            public static void Main(string[] args)
            {
                // Get a handle to the workspace if it is running
                reflectionApplication = MyReflection.ActiveApplication;
                // If workspace is not running, create an instance of the workspace
                if (reflectionApplication == null)
                {
                    reflectionApplication = MyReflection.CreateApplication("myWorkspace", true);
                }
                // Make the workspace invisible
                IFrame frame = (IFrame)reflectionApplication.GetObject("Frame");
                frame.Visible = false;
                if (reflectionApplication != null)
                {
                    string sessionPath = Environment.GetEnvironmentVariable("USERPROFILE") + @"\Documents\Micro Focus\InfoConnect\demoSession.rd3x";
                    // Create a terminal from the session document file
                    IIbmTerminal terminal =
                    (IIbmTerminal)reflectionApplication.CreateControl(sessionPath);
                    // Fire event when macro is completed
                    terminal.Macro.MacroCompleted += new EventHandler(rMacro_MacroCompleted);
                    if (terminal != null)
                    {
                        // Create a view - required for our macro
                        IView view = frame.CreateView(terminal);
                        terminal.Connect();
                        // Wait for the connection. (You can also use the AfterConnect event to do this.)
                        while (!terminal.IsConnected)
                        {
                            System.Threading.Thread.Sleep(500);
                        }
                        // Enter the password
                        terminal.Macro.PasswordBox("Enter the password", "Log on to InfoConnect");
                        // Get a list of macro names in the session document folder
                        string[] names = terminal.Macro.GetMacroNames(MacroEnumerationOption.Document);
                        foreach (string macroName in names)
                        {
                            // Print the names of the macros in the session
                            Console.WriteLine(macroName);
                            // Run this Macro if it is in the session document
                            if (macroName == "Module1.DemoMacro")
                            {
                                terminal.Macro.RunMacro(MacroEnumerationOption.Document, "Module1.DemoMacro");
                                break;
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("Can not create the control. Please check the session file path.");
                    }
                }
                else
                {
                    Console.WriteLine("Failed to get Application object.");
                }
            }
        }
    }
    
                                           
    
    Run a Macro
    Copy Code
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Attachmate.Reflection.Framework;
    using Attachmate.Reflection.Emulation.OpenSystems;
    using Attachmate.Reflection.UserInterface;
    namespace CreateSessionFromExistingSessionFile
    {
        class Program
        {
            //Declare the InfoConnect application (workspace)
            static public Attachmate.Reflection.Framework.Application reflectionApplication;
            //Close InfoConnect after the macro has completed
            static void rMacro_MacroCompleted(object sender, EventArgs e)
            {
                Console.WriteLine("Closing InfoConnect");
                reflectionApplication.Close(ApplicationCloseOption.CloseNoSave);
            }
            static void Main(string[] args)
            {
                //Get a handle to the workspace if it is running
                reflectionApplication = MyReflection.ActiveApplication;
                //If workspace is not running, create an instance of the workspace
                if (reflectionApplication == null)
                {
                    reflectionApplication = MyReflection.CreateApplication("myWorkspace", true);
                }
                if (reflectionApplication != null)
                {
                    string sessionPath = Environment.GetEnvironmentVariable("USERPROFILE") + @"\Documents\Micro Focus\InfoConnect\demoSession.rdox";
                    //Create a terminal from the session document file
                    ITerminal terminal = (ITerminal)reflectionApplication.CreateControl(sessionPath);
                    //Fire event when macro is completed
                    terminal.Macro.MacroCompleted += new EventHandler(rMacro_MacroCompleted);
                    if (terminal != null)
                    {
                        terminal.Connect();
                        //Wait for the connection. (You can also use the AfterConnect event to do this.)
                        while (!terminal.IsConnected)
                        {
                            System.Threading.Thread.Sleep(500);
                        }
                        //Get the Frame and create a view - required for our macro
                        IFrame frame = (IFrame)reflectionApplication.GetObject("Frame");
                        IView view = frame.CreateView(terminal);
                        //Wait for the screen to settle
                        terminal.Screen.WaitForHostSettle(3000);
                        //Get a list of macro names in the session document folder
                        string[] names = terminal.Macro.GetMacroNames(ProjectOption.CurrentProject);
                        foreach (string macroName in names)
                        {
                            //print the names of the macros in the session
                            Console.WriteLine(macroName);
                            //Run this macro if it is in the session document
                            if (macroName == "Module1.DemoMacro")
                            {
                                terminal.Macro.RunMacro(ProjectOption.CurrentProject, "Module1.DemoMacro");
                            }
                        }
                    }
                    else
                        Console.WriteLine("Cannot create the control. Please check the session file path.");
                }
                else
                    Console.WriteLine("Failed to get Application object.");
            }
        }
    }
                                           
                                           
           
    

     

To test this project

  • Press F5 to run the project and verify that the macro runs.