Page 1 of 1

Excel error

Posted: Wed Feb 27, 2013 11:16 pm
by nhassan
Hi Experts,

I get the following error when running the following code snippet in Ranorex 4.0.2:

"The type or namespace name 'Office' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) (CS0234)"

The code snippet is:

---------

Code: Select all


using Excel = Microsoft.Office.Interop.Excel; 

        public void Validate_cell()
        {
        	Excel.Application xlApp;
        	Excel.Workbook xlWorkBook;
        	Excel.Worksheet xlWorkSheet;
        	Excel.Range range;
        	
        	string str;
        	
        	xlApp = new Excel.ApplicationClass();
        	xlWorkBook = xlApp.Workbooks.Open("C:\Users\Administrator\Desktop\Test_DataFile.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
        	xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(Sheet1);
        	
        	range = xlWorkSheet.UsedRange;

            for (int rCnt = 2; rCnt <= range.Rows.Count; rCnt++) {
                repo.row_index = Convert.ToString(rCnt);
            	for (int cCnt = 1; cCnt <= range.Columns.Count; cCnt++) {
                    repo.col_index = Convert.ToString(cCnt);
        			Report.Log(ReportLevel.Info, "Validation", "(Optional Action)\r\nValidating AttributeEqual (Class='checkmarkIcon') on item 'MPT.Cell'.", repo.MPT.CellInfo);
            		Validate.Attribute(repo.MPT.CellInfo, "Class", "checkmarkIcon", Validate.DefaultMessage, false);        			
            		str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2 ;
                    MessageBox.Show(str);
                }
            }

            xlWorkBook.Close(true, null, null);
            xlApp.Quit();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);


Any suggestions please.

Basically, I need to go to one cell at a time and read the value and compare it to a HTML table in the application. Are there any other ways to accomplish the same task.

Thanks,

Re: Excel error

Posted: Thu Feb 28, 2013 12:17 am
by Ciege
Did you make a reference to Microsoft.Office.Interop.Excel?

Re: Excel error

Posted: Thu Feb 28, 2013 2:27 am
by nhassan
Hi Ciege,
Yes I did. It is the first line on the code.

Thanks,
nhassan

Re: Excel error

Posted: Thu Feb 28, 2013 1:19 pm
by hs.fk
I don't think Ciege means the using-directive, but a entry in the project settings, which adds a reference to the Microsoft.Office.Interop.Excel-Assembly (.dll)

How to: Add and Remove References in Visual Studio (C#, J#)

Re: Excel error

Posted: Thu Feb 28, 2013 3:37 pm
by Ciege
hs.fk wrote:I don't think Ciege means the using-directive, but a entry in the project settings, which adds a reference to the Microsoft.Office.Interop.Excel-Assembly (.dll)

How to: Add and Remove References in Visual Studio (C#, J#)
Thank you... Yes, that is what I was asking... Adding a specific reference to Microsoft.Office.Interop.Excel, not a using statement....

Re: Excel error

Posted: Fri Mar 01, 2013 3:17 pm
by nhassan
Hi,
I have the following References added to the list:
[img]
3-1-2013 9-11-18 AM.png
[/img]

I have added the following two lines in the user code:

using Excel = Microsoft.Office.Interop.Excel;

However, on running Ranorex, I get the following build error:

Namespace '<global namespace>' contains a definition conflicting with alias 'Excel' (CS0576)

on this line of the code:

Excel.Application xlApp;


Please advise.

Thanks,
nhassan

Re: Excel error

Posted: Fri Mar 01, 2013 4:23 pm
by Ciege
Try removing the Excel reference... Or change the Alias name in your code... Sounds like you have a conflict in naming...

Also, I use the Microsoft.Office.Interop.Excel reference not the Interop.Microsoft.Office.Interop.Excel reference. Not sure if that would make a difference or not...