Walkthroughs > Retrieve and Enter Data > Enter 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 enter data from Excel into 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 Ribbon.cs file to handle the button click event for the Enter Data button. This code does the work of opening InfoConnect, navigating to the data, and copying the data from Excel to the host application.
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.
Enter data from Excel to host application |
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 PutInData { 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, 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 sesion visible and get a handle to the screen IFrame frame = (IFrame)app.GetObject("Frame"); frame.CreateView(terminal); IIbmScreen screen = terminal.Screen; //Navigate to the screen that has the data entry fields screen.SendControlKey(Attachmate.Reflection.Emulation.IbmHosts.ControlKeyCode.Transmit); screen.WaitForHostSettle(2000, 3000); screen.PutText("ISPF", 23, 1); screen.SendControlKey(Attachmate.Reflection.Emulation.IbmHosts.ControlKeyCode.Transmit); screen.WaitForHostSettle(2000, 3000); screen.PutText("1", 2, 15); screen.SendControlKey(Attachmate.Reflection.Emulation.IbmHosts.ControlKeyCode.Transmit); screen.WaitForHostSettle(2000, 3000); //get the data from the Excel worksheet string[,] data = GetData(); //Enter the data into the host form EnterData(data, screen); } } } |
GetData method |
Copy Code
|
---|---|
public string[,] GetData() { //Get the range that includes all the cells used in the worksheet Excel.Worksheet myWorksheet = (Excel.Worksheet)PutInData.Globals.ThisWorkbook.ActiveSheet; Excel.Range range = myWorksheet.UsedRange; //Create an array to hold the range values String[,] rangeArray = new string[range.Rows.Count, range.Columns.Count]; //Get all the values in the range for (int rCnt = 1; rCnt <= range.Rows.Count; rCnt++) { for (int cCnt = 1; cCnt <= range.Columns.Count; cCnt++) { string cellValue = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2; rangeArray[rCnt - 1, cCnt - 1] = cellValue; } } return rangeArray; } |
EnterData method |
Copy Code
|
---|---|
public void EnterData(string[,] data, IIbmScreen screen) { for (int rCnt = 0; rCnt < data.GetLength(0); rCnt++) { //Enter the row of data in the form for (int cCnt = 0; cCnt < data.GetLength(1); cCnt++) { string input = data[rCnt, cCnt]; screen.PutText(input, (cCnt + 5), 18); } //Submit the form to add the data screen.PutText("Autoexec", 2, 15); screen.SendControlKey(ControlKeyCode.Transmit); //The Wait allows you to see the input screen.Wait(3000); //To speed this up, replace the above line with the following line //screen.WaitForHostSettle(2000, 3000); //Go back to the previous screen to enter some more data screen.SendControlKey(ControlKeyCode.F3); } } |