如何强制单元格停止在Excel互操作中进行编辑? [英] How can I force a cell to stop editing in Excel interop?

查看:113
本文介绍了如何强制单元格停止在Excel互操作中进行编辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有功能区的Excel 2007加载项.功能区中的按钮之一触发了当前工作表的大量操作,出于速度和UX的原因,我在操作期间将各种Excel.Application属性(例如InteractiveEnableEvents设置为false)设置为.

我的问题是功能区无法吸引焦点,因此,如果用户在单击我的功能区按钮时正在编辑单元格,则设置Application.Interactive = false时会引发异常,因为Excel认为用户仍在编辑单元格. /p>

是否可以通过保存或放弃对单元所做的更改来停止版本?

看来Range.DiscardChanges()可能会解决我的问题,但在2007 API中不可用.

Range.DiscardChanges()似乎是基于OLAP数据源的范围,因此无论如何也无法解决我的问题.

我尝试在另一个单元格上调用Range.Activate().虽然我确实看到GUI中的单元格焦点发生了变化,但无论如何都会抛出异常. Range.Select()什么也没做.

解决方案

因此,环顾了几天并询问了MSDN之后,最终的答案是API中根本没有用于此的东西,并且它不是可能无需使用骇客的解决方案 1 .

Excel本身处理这类事情的方式只是在用户编辑时将其菜单显示为灰色,因此我这样做是为了与宿主应用程序保持一致.

1 :hacky,因为使用SendKeys之类的技术需要返回GUI来确保操作已被处理,然后调用要求单元格未处于编辑状态的方法.在一个地方这样做很好,但是对于对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.

解决方案

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.

这篇关于如何强制单元格停止在Excel互操作中进行编辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆