简体   繁体   中英

How can I force a cell to stop editing in Excel interop?

I have an Excel 2007 add-in with a ribbon. One of the buttons in the ribbon triggers heavy manipulations of the current worksheet and I set various Excel.Application properties like Interactive and EnableEvents to false during the manipulations for speed and UX reasons.

My problem is that the ribbon doesn't steal focus so if the user was editing a cell when he clicks my ribbon button, an exception is thrown when setting Application.Interactive = false because Excel considers the user is still editing the cell.

Is there a way to stop the edition either by saving or discarding the changes made to the cell?

It appears that Range.DiscardChanges() would probably solve my problem but it isn't available in the 2007 API.

EDIT: It seems that Range.DiscardChanges() is for ranges based on OLAP data sources so it wouldn't solve my problem anyway.

EDIT: I have tried calling Range.Activate() on another cell. While I do see the cell focus changing in the GUI, the exception is thrown anyway. Range.Select() doesn't do anything.

You can not stop editing but you can check if editing is enabled and afterwards tell the user to stop it:

private bool IsEditing(Microsoft.Office.Interop.Excel.Application excelApp)
{
    if (excelApp.Interactive)
    {
        try
        {
            excelApp.Interactive = false;
            excelApp.Interactive = true;
        }
        catch (Exception)
        {
            MessageBox.Show("Please stop cell editing before you continue.");
            return true;
        }
    }
    return false;
}

I found the answer at this page and changed it from VBA to C#: https://www.add-in-express.com/creating-addins-blog/2011/03/23/excel-check-user-edit-cell/

So after looking around for a few days and asking on MSDN, the final answer is that there is simply nothing in the API for this and that it isn't possible without using hacky solutions 1 .

The way Excel itself handles this kind of thing is simply to gray out its menus while the user is editing so I've done that instead for the sake of consistency with the host application.

1 : Hacky, because techniques like using SendKeys require going back to the GUI to make sure the action is processed and then calling the method that required the cells to not be in edition. This is fine in one place, but doing that for every method call to the Excel Interop is madness.

It may be too late to answer to it. But thinking someone use this trick when they look for a solution.

For some reason, the sendkeys and Cells/Range activate command didn't workout.

The only workaround to get rid of this issue would be change the focus to another worksheet and come back to current worksheet to cheat the excel. :-)

ExcelUtil.ActivateWorksheet("Sheet1");
currentWorksheet.Activate();

public static void ActivateWorksheet(string strWorksheetName)
    {
        bool found = false;

        foreach (Excel.Worksheet sheet in Globals.ThisAddIn.Application.Worksheets)
        {
            if (sheet.Name.ToLower() == strWorksheetName.ToLower())
            {
                found = true;
                break;
            }
        }

        if (!found)
            Globals.ThisAddIn.AddNewWorksheet(strWorksheetName);

        ((Excel.Worksheet)Globals.ThisAddIn.Application.Worksheets[strWorksheetName]).Activate();
    }

Hope this helps.

Ram

如果您选择另一个单元格或所有单元格,错误会消失吗?

private void myFunction() { xlSheet.Cells.Select(); //the rest of the work }

I detected the problem and displayed a message to tell user: If excelHelper.ExcelIsEditing() Then System.Media.SystemSounds.Beep.Play() MsgBox("Please finish editing the sheet!" & vbCrLf & vbCrLf & "Move cursor out of current cell and try button again.") Exit Function End If

Function ExcelIsEditing() As Boolean ExcelIsEditing = False

  If ExcelApp.Interactive = False Then Return False
  Try
     ExcelApp.Interactive = False
     ExcelApp.Interactive = True
  Catch
     Return True
  End Try

End Function

its the best I could come up with :_(

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM