2016-05-31 8 views
0

Ich erhalte weiterhin den Fehler im Titel beim Versuch, ein neues Arbeitsblatt zu einer vorhandenen Arbeitsmappe hinzuzufügen.Fehler: HRESULT: 0x800A03EC versucht, ein neues Arbeitsblatt zu erstellen

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

Die Arbeitsmappe funktioniert anders, wenn ich diesen Kommentar aus und geben Sie in die Arbeitsmappe über das Programm, aber sobald ich diese Zeile hinzufügen, es explodiert gerade. Irgendwelche Vorschläge?

Antwort

0

Es könnte eine dieser beiden Ursachen:
1) Arbeitsmappe
Siehe geschützt How to Determine If a Workbook or a Worksheet Is Protected

2) Anwendung im Bearbeitungsmodus ist/Zustand, wenn Sie versuchen, ein neues Arbeitsblatt einfügen
(dies Modus ist aktiviert, wenn Sie in eine Zelle doppelklicken und etwas schreiben, aber noch nicht auf Enter geklickt haben)

Unten sind zwei Funktionen in C#, die prüfen, ob sich eine Excel-Anwendung im Bearbeitungsmodus befindet und versuchen, den Bearbeitungsmodus zu verlassen.

/// <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; 
    }