错误:HRESULT:0x800A03EC尝试创建新工作表

时间:2016-05-31 19:47:35

标签: vb.net excel excel-interop worksheet

在尝试将新工作表添加到现有工作簿时,我一直收到标题中的错误。

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

工作簿工作正常,否则当我发表评论并通过程序输入到工作簿时,但只要我添加这一行,它就会爆炸。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

可能有以下两个原因之一:
1)工作簿受到保护 见How to Determine If a Workbook or a Worksheet Is Protected

2)当您尝试插入新工作表时,应用程序处于编辑模式/进入模式 (当您在单元格中双击并写入某些内容时,此模式将被激活,但尚未单击“输入”)

以下是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;
    }