Home > Software design >  Compiler Error CS1061 when trying to programmatically get the top and left positions of ActiveCell i
Compiler Error CS1061 when trying to programmatically get the top and left positions of ActiveCell i

Time:01-17

//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.

CodePudding user response:

ActiveCell is no property of Worksheet objects.

It is a property of the Application object. Documentation

CodePudding user response:

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)
    {
        MessageBox.Show(activeCell.Value2);
        //Get the top and left position of the active cell
        double top = (double)activeCell.Top;
        double left = (double)activeCell.Left;
    }
  • Related