Microsoft.Office.Interop.Excel: Method 'Cells' not found

Class library usage, coding and language questions.
User avatar
krupaluk
Posts: 5
Joined: Mon Apr 18, 2016 7:24 pm

Microsoft.Office.Interop.Excel: Method 'Cells' not found

Post by krupaluk » Tue Apr 19, 2016 11:08 am

Hello,

following code works well for me in Ms Visual Studio Express but unfortunately not in Ranorex.

Code: Select all

 /*
 * Created by Ranorex
 * User: Drake
 * Date: 4/19/2016
 * Time: 6:00 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Threading;
using WinForms = System.Windows.Forms;
using Ranorex;
using Ranorex.Core;
using Ranorex.Core.Testing;
using Microsoft.Office.Interop.Excel;

namespace lab
{
    /// <summary>
    /// Description of ExcInt.
    /// </summary>
    [TestModule("418266C8-4FB9-4B06-A823-044A2DFEFBDF", ModuleType.UserCode, 1)]
    public class ExcInt : ITestModule
    {
        /// <summary>
        /// Constructs a new instance.
        /// </summary>
        public ExcInt()
        {
            // Do not delete - a parameterless constructor is required!
        }

        /// <summary>
        /// Performs the playback of actions in this module.
        /// </summary>
        /// <remarks>You should not call this method directly, instead pass the module
        /// instance to the <see cref="TestModuleRunner.Run(ITestModule)"/> method
        /// that will in turn invoke this method.</remarks>
        void ITestModule.Run()
        {
            Mouse.DefaultMoveTime = 300;
            Keyboard.DefaultKeyPressTime = 100;
            Delay.SpeedFactor = 1.0;
            
            String dtPath = "d:\\ranorexTutorial\\dtLab.xlsx";
            String dtSheet = "labSheet01";

            Microsoft.Office.Interop.Excel.Application objExcel = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook objBook = objExcel.Workbooks.Open(dtPath);
            var objSheet = objBook.Worksheets[dtSheet];
            System.Windows.Forms.MessageBox.Show(objSheet.Cells(2, 2).Text);
        }
    }
}


In Ranorex, I receive following error:
'object' does not contain a definition for 'Cells' and no extension method 'Cells' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) (CS1061) - D:\ranorexTutorial\lab\lab\excInt.cs:55,59

I use identical Microsoft.Office.Interop.Excel dll file as source.

Here, see snapShots from Ms Visual Studio and from Ranorex when trying to run the code:

Ms Visual Studio (without error):
vStudio.jpg
vStudio.jpg (80.66 KiB) Viewed 4568 times
Ranorex (with error):
ranorex.jpg
ranorex.jpg (103.74 KiB) Viewed 4568 times
Thank you!

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 3908
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Microsoft.Office.Interop.Excel: Method 'Cells' not found

Post by odklizec » Tue Apr 19, 2016 12:43 pm

Hi,

Try this code instead:

Code: Select all

            String dtPath = "d:\\ranorexTutorial\\dtLab.xlsx";
            String dtSheet = "labSheet01";
            Microsoft.Office.Interop.Excel.Application objExcel = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook objBook = objExcel.Workbooks.Open(dtPath);
            Excel.Worksheet objSheet = (Excel.Worksheet)objBook.Worksheets[dtSheet];
            Excel.Range objRange = (Excel.Range)objSheet.Cells[2,2];
            System.Windows.Forms.MessageBox.Show(objRange.Value.ToString());
            objBook.Close();
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

User avatar
krupaluk
Posts: 5
Joined: Mon Apr 18, 2016 7:24 pm

Re: Microsoft.Office.Interop.Excel: Method 'Cells' not found

Post by krupaluk » Tue Apr 19, 2016 1:36 pm

Thank you odklizec, however, yes... I know that code, I know that I can use Excel.Range.

My point was to find out the difference why the same C# code works in Ms Visual Studio but not in Ranorex. that's complete mystery to me. The only thing I can think of is that Ranorex sees it different way than Ms Visual Studio. But how can I make it work in Ranorex?

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: Microsoft.Office.Interop.Excel: Method 'Cells' not found

Post by krstcs » Tue Apr 19, 2016 2:55 pm

What version of .NET are you trying to use?

Note that Ranorex Studio is built on SharpDevelop 3.2, which is very old and was built back when .NET 3.5 was "new", so there are a lot of issues with trying to use .NET 4.5 or 4.6 with Ranorex right now. It usually will still work/compile, but Ranorex's code completion/Intellisense will not work correctly and it will show errors when there aren't any. I've had issues with some network/service level code from 4.5 that is similar to what you describe. I had to go back to 4.0 and work around the issue using 4.0 style libraries.

If you try .NET 3.5 or 4.0, do you get the same issue? (Your library may require 4.5, I'm not sure.)


Ranorex 6.0 will be built on SharpDevelop 4.x, so it will be much better when used with newer versions of .NET. 6.0 is currently in closed beta, but should be released soon (my guess is by the end of June, unless there is a show-stopping bug found before then).
Shortcuts usually aren't...

User avatar
krupaluk
Posts: 5
Joined: Mon Apr 18, 2016 7:24 pm

Re: Microsoft.Office.Interop.Excel: Method 'Cells' not found

Post by krupaluk » Tue Apr 19, 2016 3:49 pm

Hi krstcs,

I think that might be the problem. When I go to Ranorex Menu/About, I see that .NET version is 4.0.3xxx something.

Thanks :-)