Excel vsto结束编辑模式
在一个Excel的COM插件,我需要访问pageSetup属性。 但是,如果在Excel中编辑模式是活跃的,我得到一个exception。
我可以用这个代码检查编辑模式是否有效:
CommandBarControl oNewMenu = excelApp.CommandBars["Worksheet Menu Bar"].FindControl( 1, //the type of item to look for 18, //the item to look for refmissing, //the tag property (in this case missing) refmissing, //the visible property (in this case missing) true); //we want to look for it recursively so the last argument should be true. if ( oNewMenu != null ) { // edit mode = true if (!oNewMenu.Enabled) { } }
我发现一些解决scheme退出编辑模式,但他们不工作:
SendKeys.Flush(); excelApplication.SendKeys("{ENTER}");
如何退出编辑模式,以便我可以编写pageSetup属性?
如果您正在使用添加,您可以尝试使用此function退出编辑模式:
Globals.ThisAddIn.Application.SendKeys("{ENTER}");
我会推荐类似于这个方法来确定Excel是否处于编辑模式:
public static void VerifyExcelIsNotInCellEditMode() { if (Globals.ThisWorkbook.Application.Interactive) { try { //Will throw an error if user is editing cell Globals.ThisWorkbook.Application.Interactive = false; Globals.ThisWorkbook.Application.Interactive = true; } catch { throw new Exception("Excel is in Edit Mode."); } } }