This sample opens an Excel spreadsheet and copies data from a terminal screen in the InfoConnect demo program to the spreadsheet row by row until it encounters an empty row. The GetText method is used to copy the data.
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 screen-scrape-data-to-a-spreadsheet.rd3x (IBM) and screen-scrape-data-to-a-spreadsheet.rdox files. The download package contains everything you need to run the macros in these files. See Download the VBA Sample Sessions.
The screen that displays the data must be visible in the terminal window when you run the macro.
First, this sample opens Excel, adds a new workbook, and then activates a sheet in the workbook.
Open Excel workbook and activate a sheet |
Copy Code
|
---|---|
Dim ExcelApp As Object Dim wkBook As Object Set ExcelApp = CreateObject("Excel.Application") ExcelApp.Visible = True Set wkBook = ExcelApp.Workbooks.Add wkBook.Sheets("sheet1").Activate |
Then it initializes the row variable to the first row it needs to copy and starts a loop to copy data until an empty row is encountered. The GetText method is used to get a row of data from the screen as a text string. This method retrieves a string of text from the screen, given starting row and column values and a length.
After the macro gets the text, it combines labels with multiple words into single words, removes extra spaces between words, and then creates an array.
Finally, the values from the array are copied into the Excel workbook. First the "_" added between compound words with spaces is replaced, and then the array values are put into the corresponding columns in the Excel worksheet.
Copy values into Excel |
Copy Code
|
---|---|
For col = LBound(rowFromHost) To UBound(rowFromHost) 'Replace delimiter rowFromHost(col) = Replace(rowFromHost(col), "_", " ", 1, 1) 'Write row to spreadsheet wkBook.Sheets("sheet1").cells(row, (col + 5)).value = rowFromHost(col) Next col |