Там может быть один из этих двух причин:
1) книга защищена
См How to Determine If a Workbook or a Worksheet Is Protected
2) Применение в режиме редактирования/Enter Mode при попытке вставить новый лист
(это режим активируется, когда вы дважды щелкаете в ячейке и что-то пишете, но не нажимаете 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;
}