Excel error

Class library usage, coding and language questions.
nhassan
Posts: 10
Joined: Tue Jan 22, 2013 10:05 pm

Excel error

Post by nhassan » Wed Feb 27, 2013 11:16 pm

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,

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Excel error

Post by Ciege » Thu Feb 28, 2013 12:17 am

Did you make a reference to Microsoft.Office.Interop.Excel?
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

nhassan
Posts: 10
Joined: Tue Jan 22, 2013 10:05 pm

Re: Excel error

Post by nhassan » Thu Feb 28, 2013 2:27 am

Hi Ciege,
Yes I did. It is the first line on the code.

Thanks,
nhassan

hs.fk
Posts: 12
Joined: Thu Jul 12, 2012 11:47 am
Location: Hamburg

Re: Excel error

Post by hs.fk » Thu Feb 28, 2013 1:19 pm

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#)

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Excel error

Post by Ciege » Thu Feb 28, 2013 3:37 pm

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....
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

nhassan
Posts: 10
Joined: Tue Jan 22, 2013 10:05 pm

Re: Excel error

Post by nhassan » Fri Mar 01, 2013 3:17 pm

Hi,
I have the following References added to the list:
[img]
3-1-2013 9-11-18 AM.png
3-1-2013 9-11-18 AM.png (9.85 KiB) Viewed 2968 times
[/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

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Excel error

Post by Ciege » Fri Mar 01, 2013 4:23 pm

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...
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...