system._comobject,while reading excel file

Experiences, small talk, and other automation gossip.
kmaheshguptha
Posts: 1
Joined: Tue Jul 15, 2014 7:06 am

system._comobject,while reading excel file

Post by kmaheshguptha » Tue Jul 15, 2014 9:32 am

Hi friends,when I am executing following script,the content read from the excel showing as system._comObject.
can anyonce help in this.....

xlRange.Cells[i,j].ToString());
when I googled it I got the solution as xlRange.Cells[i,j].value2.ToString());
but
value2 fun/method/command not showing in the popup that comes on putting dot after xlRange.Cells[i,j].

using System;
using System.Threading;
using System.Drawing;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using WinForms = System.Windows.Forms;

using Ranorex;
using Ranorex.Core;
using Ranorex.Core.Reporting;
using Ranorex.Core.Testing;
using System.Windows.Forms;
using Office;
using Microsoft.Office.Interop.Excel;


namespace excelReadNDwrite
{
class Program
{
[STAThread]
public static void Main(string[] args)
{
Keyboard.AbortKey = System.Windows.Forms.Keys.Pause;
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook =xlApp.Workbooks.Open(@"E:\MyExcelTest\keywordDriven.xls");
Excel.Worksheet xlWorkSheet = (Excel.Worksheet)(xlWorkbook.Worksheets.get_Item(3));
Excel.Range xlRange = xlWorkSheet.UsedRange;
int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;
MessageBox.Show(rowCount.ToString());
MessageBox.Show(colCount.ToString());
for (int i = 1; i <=3; i++)
{
for (int j = 1; j <= 3; j++)
{
MessageBox.Show(xlRange.Cells[i,j].ToString());
}
}

}
}
}

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: system._comobject,while reading excel file

Post by Support Team » Mon Jul 28, 2014 4:05 pm

Hi kmaheshguptha,

This is not really a Ranorex issue, but I think you have to cast the cell to a Excel.Range.
Please try the following code snippet in your for loop:
string myValue = (string)(xlRange.Cells[i,j] as Excel.Range).Value2;
MessageBox.Show(myValue);
Regards,
Bernhard