2016-05-31 32 views
0

既存のワークブックに新しいワークシートを追加しようとすると、タイトルにエラーが表示され続けます。私はこれをコメントアウトし、プログラムを介したワークブックに入力、しかし、すぐに、私はこの行を追加すると、それだけで爆発するそれ以外のときエラー:HRESULT:0x800A03EC新しいワークシートを作成しようとしました

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

ワークブックが正常に動作します。助言がありますか?

答えて

0

これら2つの原因の一つがあるかもしれません:(
1)ワークブックがHow to Determine If a Workbook or a Worksheet Is Protected

2)アプリケーションでは、新しいワークシート
を挿入しようとすると、/モードを入力し、編集モードにある参照してください
を保護され、このモードは、セルをダブルクリックして何かを書くが、Enterをまだクリックしていないときにアクティブになる)

以下は、Excelアプリケーションが編集モードにあるかどうかをチェックし、編集モードを終了しようとする2つの関数です。

/// <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; 
    } 
関連する問題