2016-05-31 74 views
0

嘗試將新工作表添加到現有工作簿時,我不斷收到標題中的錯誤。否則,當我評論了這一點,並輸入到通過程序的工作簿,但只要我加入這行,它只是爆炸錯誤:HRESULT:0x800A03EC嘗試創建新工作表

Dim xlApp As New Excel.Application 
Dim ws2 As Excel.Worksheet 
    ws2 = xlApp.Worksheets.Add 
    ws2.Name = "Sheet2" 

工作簿工作正常。有什麼建議麼?

回答

0

有可能是這兩種原因之一:
1)保護工作簿
How to Determine If a Workbook or a Worksheet Is Protected

2)應用在編輯模式下,當您嘗試插入新工作表
/輸入模式(此模式在您單擊某個單元格並雙擊時激活,但尚未單擊Enter鍵)

下面是C#中的兩個函數,用於檢查Excel應用程序是否處於編輯模式並嘗試退出編輯模式。

/// <summary> 
    /// <para>Checks if <paramref name="inputApp"/> is in Edit Mode (aka. Enter Mode)</para> 
    /// </summary> 
    /// <param name="inputApp"></param> 
    /// <returns>true if this Excel application is in Edit Mode, otherwise false</returns> 
    /// <remarks>Edit Mode is when a cell value gets edited and user doesn't press Enter/clicks on tick button from formula bar</remarks> 
    public static bool IsInEditMode(this Microsoft.Office.Interop.Excel.Application inputApp) 
    { 
     if (inputApp.Interactive == false) 
     { 
      return false; 
     } 
     else 
     { 
      try 
      { 
       inputApp.Interactive = false; 
       inputApp.Interactive = true; 

       return false; 
      } 
      catch (Exception ex) 
      { 
       return true; 
      } 
     } 
    } 

    /// <summary> 
    /// <para>Tries to exit from Edit Mode for <paramref name="inputApp"/> by switching to another worksheet and coming back</para> 
    /// </summary> 
    /// <param name="inputApp"></param> 
    /// <returns>true if this function succeeded to exit Edit Mode, otherwise false</returns> 
    /// <remarks>In order to work, there have to be at least two worksheets in the workbook</remarks> 
    public static bool ExitEditMode(this Microsoft.Office.Interop.Excel.Application inputApp) 
    { 
     bool result = true; 
     bool screenUpdatingBeforeValue = Globals.ThisAddIn.Application.ScreenUpdating; 
     bool enableEventsBeforeValue = Globals.ThisAddIn.Application.EnableEvents; 

     try 
     { 
      Globals.ThisAddIn.Application.ScreenUpdating = false; 
      Globals.ThisAddIn.Application.EnableEvents = false; 

      if (Globals.ThisAddIn.Application.Worksheets.Count > 1) 
      { 
       Microsoft.Office.Interop.Excel.Worksheet currentActiveSheet = (Microsoft.Office.Interop.Excel.Worksheet)Globals.ThisAddIn.Application.ActiveSheet; 

       for (int i = 1; i <= Globals.ThisAddIn.Application.Worksheets.Count; i++) 
       { 
        if (currentActiveSheet.Index == i) 
         continue; 
        else 
        { 
         Microsoft.Office.Interop.Excel.Worksheet currentSheet = Globals.ThisAddIn.Application.Worksheets.Item[i]; 
         currentSheet.Activate(); 
         currentActiveSheet.Activate(); 
         break; 
        } 
       } 
      } 
      else 
      { 
       result = false; 
      } 
     } 
     catch (Exception ex) 
     { 
      // something went wrong 
      result = false; 
     } 
     finally 
     { 
      Globals.ThisAddIn.Application.ScreenUpdating = screenUpdatingBeforeValue; 
      Globals.ThisAddIn.Application.EnableEvents = enableEventsBeforeValue; 
     } 

     return result; 
    } 
相關問題