2014-03-03 55 views
2

在一個excel COM插件中我需要訪問pageSetup屬性。但是如果excel中的編輯模式是活動的,我會得到一個異常。Excel vsto結束編輯模式

我可以檢查,如果編輯模式是與此代碼的主動:

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) { 
     } 
    } 

我已經找到了一些解決方案,以退出編輯模式,但他們沒有工作:

SendKeys.Flush(); 
excelApplication.SendKeys("{ENTER}"); 

哪有我退出編輯模式,以便我可以編寫pageSetup屬性?

回答

1

您可以嘗試使用此退出,如果你使用的是附加在編輯模式:

​​

而且我會推薦與此類似這種方法的財產以後,以確定是否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."); 
     } 
    } 
}