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

谁说胖子不能爱 提交于 2019-12-11 03:24:50

问题


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.


回答1:


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

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.




回答2:


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/




回答3:


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




回答4:


If you select another cell or all cells, does the error disappear?

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




回答5:


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 :_(



来源:https://stackoverflow.com/questions/22482935/how-can-i-force-a-cell-to-stop-editing-in-excel-interop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!