Walkthroughs > Retrieve and Enter Data > Get Data With a Visual Studio Office App |
This walkthrough shows how to create a Microsoft Visual Studio Office solution that can be used to open InfoConnect and get data from a host application. You can use this process to develop an Excel application level add-in that includes these features for any Excel workbook or to customize an Excel workbook so that they are available only when that workbook is open.
Next, add the controls you want users to see in the Excel Ribbon when they open the document.
Now that you have designed the Ribbon for the workbook, set up the Ribbon1.cs file to handle the button click event for the Get Data button. This code does the work of opening InfoConnect, navigating to the data, and copying the data from the host to Excel.
You can follow along with this section or skip to Ribbon1.cs code at the end of this article and copy all of the code to Ribbon1.cs.
This code starts InfoConnect and creates an event handler for the NewScreenReady event, which navigates to the screen that has the data. When that screen is ready, the event handler calls methods to get the data from the screen and add the data to Excel.
Create a session, navigate to a screen, and call methods to get and enter data |
Copy Code
|
---|---|
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Office.Tools.Ribbon; using Excel = Microsoft.Office.Interop.Excel; using Attachmate.Reflection.UserInterface; using Attachmate.Reflection; using Attachmate.Reflection.Framework; using Attachmate.Reflection.Emulation.IbmHosts; namespace VSTOGetScreenData { public partial class Ribbon1 { private void Ribbon1_Load(object sender, RibbonUIEventArgs e) { } private void button1_Click(object sender, RibbonControlEventArgs e) { //Start a visible instance of InfoConnect or get the instance running at the given channel name Application app = MyReflection.CreateApplication("MyWorkspace", true); //Create a terminal control, configure, and connect IIbmTerminal terminal = (IIbmTerminal)app.CreateControl(new Guid("{09E5A1B4-0BA6-4546-A27D-FE4762B7ACE1}")); terminal.HostAddress = "demo:ibm3270.sim"; terminal.Port = 623; terminal.Connect(); //Make the session visible and then get a handle to the screen IFrame frame = (IFrame) app.GetObject("Frame"); frame.CreateView(terminal); IIbmScreen screen = terminal.Screen; //Add an event handler to navigate screens screen.NewScreenReady += screen_NewScreenReady; } void screen_NewScreenReady(object sender, EventArgs e) { String screenID1, screenID2, screenID3; IIbmScreen screen = (IIbmScreen) sender; //Get text at specific locations and compare with known text to identify screen screenID1 = screen.GetText(1, 2, 6); screenID2 = screen.GetText(1, 7, 4); screenID3 = screen.GetText(1, 25, 13); if (screenID1 == "ATM VM") { screen.SendControlKey(Attachmate.Reflection.Emulation.IbmHosts.ControlKeyCode.Transmit); } if (screenID2 == "ATM5") { screen.PutText("kayak", 23, 1); screen.SendControlKey(Attachmate.Reflection.Emulation.IbmHosts.ControlKeyCode.Transmit); } if (screenID3 == "INTERNATIONAL") { //Get the screen data from the application int[,]screenData = GetScreenData(screen); //Put the data into Excel putInData(screenData); } } } } |
This code starts InfoConnect, opens a session document file, and navigates to the screen that has the data. Then it calls the GetScreenData() method to get the data from the screen and the PutDataIntoExcel() method to put it into an Excel worksheet.
Open session, navigate to a screen, and call methods to get and enter data |
Copy Code
|
---|---|
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Office.Tools.Ribbon; using Excel = Microsoft.Office.Interop.Excel; using Attachmate.Reflection.UserInterface; using Attachmate.Reflection; using Attachmate.Reflection.Framework; using Attachmate.Reflection.Emulation.OpenSystems; namespace ExcelWorkBookOS { public partial class Ribbon1 { private void Ribbon1_Load(object sender, RibbonUIEventArgs e) { } private void button1_Click(object sender, RibbonControlEventArgs e) { //Start a visible instance of InfoConnect or get the instance running at the given channel name Application reflectionApplication = MyReflection.CreateApplication("MyWorkspace", true); //Create a terminal from the session document file string sessionPath = Environment.GetEnvironmentVariable("USERPROFILE") + @"\Documents\Attachmate\InfoConnect\demoSession.rdox"; ITerminal terminal = (ITerminal)reflectionApplication.CreateControl(sessionPath); //Make the session visible in the workspace and get a handle to the screen IFrame frame = (IFrame)reflectionApplication.GetObject("Frame"); frame.CreateView(terminal); IScreen screen = terminal.Screen; //Navigate to the screen that has the data screen.SendKeys("un"); screen.SendControlKey(ControlKeyCode.Enter); screen.SendKeys("pw"); screen.SendControlKey(ControlKeyCode.Enter); //wait for the host before entering more data screen.Wait(3000); screen.SendKeys("demodata"); screen.SendControlKey(ControlKeyCode.Enter); screen.Wait(3000); //Get the starting point for the data ScreenPoint point = screen.SearchText("Jan", 1, 1, FindOptions.Forward); int row = point.Row; int col = point.Column + 3; //Get the Data int[,] dataRange = GetScreenData(row, col, screen); //Put the data into Excel PutDataIntoExcel(dataRange); } } } |
Get the data |
Copy Code
|
---|---|
public int[,] GetScreenData(IIbmScreen screen) { //Start on row 8 and get the first row of text from the screen int row = 8; string rowText = screen.GetText(row, 26, 50); //Create a list of lists to hold the data for each row List<List<string>> list = new List<List<string>>(); //Gather data until an empty row is encountered while (!string.IsNullOrWhiteSpace(rowText)) { //Replace spaces between compound words in first column and remove extra spaces while (rowText.Contains(" ")) { rowText = rowText.Replace(" ", " "); } rowText = rowText.Replace(",", ""); rowText = rowText.Trim(); List<string> rowList = rowText.Split(' ').ToList(); list.Add(rowList); row++; //Get the new row of text from the screen rowText = screen.GetText(row, 26, 50); } //Convert list of lists into a two dimensional array var realDataRange = new int[list.Count, list[0].Count]; for (int i = 0; i < list.Count; i++) { for (int j = 0; j < list[0].Count; j++) { realDataRange[i, j] = int.Parse(list[i][j]); } } return realDataRange; } |
Get the data |
Copy Code
|
---|---|
public int[,] GetScreenData(int row, int col, IScreen screen) { string rowText = screen.GetText(row, col, 50); List<List<string>> list = new List<List<string>>(); //Gather data until an empty row is encountered while (!rowText.Contains("-")) { //Replace spaces between compound words in first column and remove extra spaces rowText = rowText.Replace("|", " "); while (rowText.Contains(" ")) { rowText = rowText.Replace(" ", " "); } rowText = rowText.Trim(); List<string> rowList = rowText.Split(' ').ToList(); list.Add(rowList); row++; //Get the new row of text from the screen rowText = screen.GetText(row, col, 50); } //Convert list of lists into a two dimenisional array and return the array var data = new int[list.Count, list[0].Count]; for (int i = 0; i < list.Count; i++) { for (int j = 0; j < list[0].Count; j++) { data[i, j] = int.Parse(list[i][j]); } } return data; } |
Put the data into Excel |
Copy Code
|
---|---|
public void putInData(int[,] screenData) { Excel.Worksheet myWorksheet = (Excel.Worksheet)VSTOGetScreenData.Globals.ThisWorkbook.ActiveSheet; // Create a Range of the correct size: int rows = screenData.GetLength(0); int columns = screenData.GetLength(1); Excel.Range range = myWorksheet.get_Range("B3", Type.Missing); range = range.get_Resize(rows, columns); range.NumberFormat = "#,##0.00"; // Assign the Array to the Range range.set_Value(Type.Missing, screenData); } |
Put the data into Excel |
Copy Code
|
---|---|
public void PutDataIntoExcel(int[,] realDataRange) { Excel.Worksheet myWorksheet = (Excel.Worksheet)ExcelWorkBookOS.Globals.ThisWorkbook.ActiveSheet; // Create a Range of the correct size: int rows = realDataRange.GetLength(0); int columns = realDataRange.GetLength(1); Excel.Range range = myWorksheet.get_Range("B3", Type.Missing); range = range.get_Resize(rows, columns); range.NumberFormat = "#,##0.00"; // Assign the Array to the Range range.set_Value(Type.Missing, realDataRange); } |
Get data with Visual Studio for Office App |
Copy Code
|
---|---|
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Office.Tools.Ribbon; using Excel = Microsoft.Office.Interop.Excel; using Attachmate.Reflection.UserInterface; using Attachmate.Reflection; using Attachmate.Reflection.Framework; using Attachmate.Reflection.Emulation.IbmHosts; namespace VSTOGetScreenData { public partial class Ribbon1 { private void Ribbon1_Load(object sender, RibbonUIEventArgs e) { } public int[,] GetScreenData(IIbmScreen screen) { //Start on row 8 and get the first row of text from the screen int row = 8; string rowText = screen.GetText(row, 26, 50); //Create a list of lists to hold the data for each row List<List<string>> list = new List<List<string>>(); //Gather data until an empty row is encountered while (!string.IsNullOrWhiteSpace(rowText)) { //Replace spaces between compound words in first column and remove extra spaces while (rowText.Contains(" ")) { rowText = rowText.Replace(" ", " "); } rowText = rowText.Replace(",", ""); rowText = rowText.Trim(); List<string> rowList = rowText.Split(' ').ToList(); list.Add(rowList); row++; //Get the new row of text from the screen rowText = screen.GetText(row, 26, 50); } //Convert list of lists into a two dimenisional array var realDataRange = new int[list.Count, list[0].Count]; for (int i = 0; i < list.Count; i++) { for (int j = 0; j < list[0].Count; j++) { realDataRange[i, j] = int.Parse(list[i][j]); } } return realDataRange; } public void putInData(int[,] screenData) { Excel.Worksheet myWorksheet = (Excel.Worksheet)VSTOGetScreenData.Globals.ThisWorkbook.ActiveSheet; // Create a Range of the correct size: int rows = screenData.GetLength(0); int columns = screenData.GetLength(1); Excel.Range range = myWorksheet.get_Range("B3", Type.Missing); range = range.get_Resize(rows, columns); range.NumberFormat = "#,##0.00"; // Assign the Array to the Range range.set_Value(Type.Missing, screenData); } private void button1_Click(object sender, RibbonControlEventArgs e) { //Start a visible instance of InfoConnect or get the instance running at the given channel name Application app = MyReflection.CreateApplication("MyWorkspace", true); //Create a terminal control, configure, and connect IIbmTerminal terminal = (IIbmTerminal)app.CreateControl(new Guid("{09E5A1B4-0BA6-4546-A27D-FE4762B7ACE1}")); terminal.HostAddress = "demo:ibm3270.sim"; terminal.Port = 623; terminal.Connect(); //Make the session visible and then get a handle to the screen IFrame frame = (IFrame) app.GetObject("Frame"); frame.CreateView(terminal); IIbmScreen screen = terminal.Screen; //Add an event handler to navigate screens screen.NewScreenReady += screen_NewScreenReady; } void screen_NewScreenReady(object sender, EventArgs e) { String screenID1, screenID2, screenID3; IIbmScreen screen = (IIbmScreen) sender; //Get text at specific locations and compare with known text to identify screen screenID1 = screen.GetText(1, 2, 6); screenID2 = screen.GetText(1, 7, 4); screenID3 = screen.GetText(1, 25, 13); if (screenID1 == "ATM VM") { screen.SendControlKey(Attachmate.Reflection.Emulation.IbmHosts.ControlKeyCode.Transmit); } if (screenID2 == "ATM5") { screen.PutText("kayak", 23, 1); screen.SendControlKey(Attachmate.Reflection.Emulation.IbmHosts.ControlKeyCode.Transmit); } if (screenID3 == "INTERNATIONAL") { //Get the screen data from the application int[,]screenData = GetScreenData(screen); //Put the data into Excel putInData(screenData); } } } } |
Get data with Visual Studio for Office App |
Copy Code
|
---|---|
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Office.Tools.Ribbon; using Excel = Microsoft.Office.Interop.Excel; using Attachmate.Reflection.UserInterface; using Attachmate.Reflection; using Attachmate.Reflection.Framework; using Attachmate.Reflection.Emulation.OpenSystems; namespace ExcelWorkBookOS { public partial class Ribbon1 { public int[,] GetScreenData(int row, int col, IScreen screen) { string rowText = screen.GetText(row, col, 50); List<List<string>> list = new List<List<string>>(); //Gather data until an empty row is encountered while (!rowText.Contains("-")) { //Replace spaces between compound words in first column and remove extra spaces rowText = rowText.Replace("|", " "); while (rowText.Contains(" ")) { rowText = rowText.Replace(" ", " "); } rowText = rowText.Trim(); List<string> rowList = rowText.Split(' ').ToList(); list.Add(rowList); row++; //Get the new row of text from the screen rowText = screen.GetText(row, col, 50); } //Convert list of lists into a two dimenisional array and return the array var data = new int[list.Count, list[0].Count]; for (int i = 0; i < list.Count; i++) { for (int j = 0; j < list[0].Count; j++) { data[i, j] = int.Parse(list[i][j]); } } return data; } public void PutDataIntoExcel(int[,] realDataRange) { Excel.Worksheet myWorksheet = (Excel.Worksheet)ExcelWorkBookOS.Globals.ThisWorkbook.ActiveSheet; // Create a Range of the correct size: int rows = realDataRange.GetLength(0); int columns = realDataRange.GetLength(1); Excel.Range range = myWorksheet.get_Range("B3", Type.Missing); range = range.get_Resize(rows, columns); range.NumberFormat = "#,##0.00"; // Assign the Array to the Range range.set_Value(Type.Missing, realDataRange); } private void Ribbon1_Load(object sender, RibbonUIEventArgs e) { } private void button1_Click(object sender, RibbonControlEventArgs e) { //Start a visible instance of InfoConnect or get the instance running at the given channel name Application reflectionApplication = MyReflection.CreateApplication("MyWorkspace", true); //Create a terminal from the session document file string sessionPath = Environment.GetEnvironmentVariable("USERPROFILE") + @"\Documents\Attachmate\InfoConnect\demoSession.rdox"; ITerminal terminal = (ITerminal)reflectionApplication.CreateControl(sessionPath); //Make the session visible in the workspace and get a handle to the screen IFrame frame = (IFrame)reflectionApplication.GetObject("Frame"); frame.CreateView(terminal); IScreen screen = terminal.Screen; //Navigate to the screen that has the data screen.SendKeys("un"); screen.SendControlKey(ControlKeyCode.Enter); screen.SendKeys("pw"); screen.SendControlKey(ControlKeyCode.Enter); //wait for the host before entering more data screen.Wait(3000); screen.SendKeys("demodata"); screen.SendControlKey(ControlKeyCode.Enter); screen.Wait(3000); //Get the starting point for the data ScreenPoint point = screen.SearchText("Jan", 1, 1, FindOptions.Forward); int row = point.Row; int col = point.Column + 3; //Get the Data int[,] dataRange = GetScreenData(row, col, screen); //Put the data into Excel PutDataIntoExcel(dataRange); } } } |