HamsterRoll
HamsterRoll

Reputation: 107

Compiler error CS1061 when trying to access the ActiveCell from worksheet in my VSTO excel application

Compiler error CS1061 when trying to programmatically get the top and left positions of ActiveCell in Excel VSTO using C#

//using Microsoft.Office.Interop.Excel;
//Create a reference to the active Excel worksheet 
Worksheet activeWorksheet = (Worksheet)Globals.ThisAddIn.Application.ActiveSheet;

//Get the top and left position of the active cell
int topPosition = activeWorksheet.ActiveCell.Top;
int leftPosition = activeWorksheet.ActiveCell.Left;

The compiler complains about the activeCell, that the worksheet has no definition and the value is not available. has anyone found a solution to that issue? I did test my code when making sure there is an active cell but still no use.

The error message is as follows:

CS1061: 'Worksheet' does not contain a definition for 'ActiveCell' and no accessible extension method 'ActiveCell' accepting a first argument of type 'Worksheet' could be found (are you missing using a directive or an assembly reference?)

Upvotes: 1

Views: 314

Answers (2)

HamsterRoll
HamsterRoll

Reputation: 107

Thanks a lot for pointing out. Defenitely there was a lot of confusion around that from other articles and answers.

Here I post an answer after I got my code finally working:

//Create a reference to the active Cell in the worksheet
Microsoft.Office.Interop.Excel.Range activeCell = this.Application.ActiveCell;
    if(activeCell  != null)
    {
        //Test that the value of the active cell is showing in a messagebox
        MessageBox.Show(activeCell.Value2);
        //Get the top and left position of the active cell
        double top = (double)activeCell.Top;
        double left = (double)activeCell.Left;
    }

Upvotes: 0

Axel Kemper
Axel Kemper

Reputation: 11322

ActiveCell is no property of Worksheet objects.

It is a property of the Application object. Documentation

Upvotes: 2

Related Questions