Home » excel » c# – Get value of Excel Cell still being edited

c# – Get value of Excel Cell still being edited

Posted by: admin May 14, 2020 Leave a comment


I need to capture the value contained in any cell or cells that are selected in my Excel add in. I’m currently using …ActiveWindow.Selection and get_Value() on that to get multiple cells selected at once and …ActiveCell.Value to get a single cell being selected. My issue is that if you input a single value in a single cell and click the button, which captures the values, everything comes back null if that cell is still being edited (the cursor is still in the cell but you have input the value). Does anyone know of a way to capture this value? I can already see it being a source of confusion for my users when trying to enter a single value.

Requested Clarification:

var selectedCell = Globals.ThisAddIn.Application.ActiveCell.Value;

If I enter a value in this cell and immediately click the button, this value is null because the cell is in edit mode and not selected mode. To make it work I have to click off of the cell and then single click it to select. Think of the different states of a cell… if you double click it, it’s in edit mode. If you click once, it is selected. I want the value of a cell that is still in edit mode.

How to&Answers:

We struggled with the same issue in our VSTO add-in. After doing a lot of research we concluded there is no way in the VSTO api to get the value of a cell that is still in edit mode. We ended up hacking the following solution: whenever a button is pressed, it checks if Excel is in edit mode, and if it is, it pops up a dialog telling the user they must exit edit mode before pressing the button.

The code to check if you are in edit mode is:

public bool IsInEditMode()
    const int menuItemType = 1;
    const int newMenuId = 18;

    CommandBarControl newMenu =
        Application.CommandBars["Worksheet Menu Bar"].FindControl(menuItemType, newMenuId, Type.Missing, Type.Missing, true);

    return newMenu != null && !newMenu.Enabled;

You can see more info at: How to tell if Excel Application is in cell-edit mode?


Use the following C# code to exit out of cell Edit mode. It works :

private void exitEditMode()
    if (!isExcelInteractive())
        // get the current range
        Excel.Range r = Globals.ThisAddIn.Application.ActiveCell;
        // bring Excel to the foreground, with focus
        // and issue keys to exit the cell
        // now make sure the original cell is
        // selected…

private bool isExcelInteractive()
        // this line does nothing if Excel is not
        // in edit mode. However, trying to set
        // this property while Excel is in edit
        // cell mdoe will cause an exception
        Globals.ThisAddIn.Application.Interactive = Globals.ThisAddIn.Application.Interactive;
        return true; // no exception, ecel is 
        // interactive
        return false; // in edit mode

private void xlBringToFront()

public static extern Int32 SetForegroundWindow(int hWnd);