Excel vsto 结束编辑模式

Ger*_*per 6 .net com excel vsto

在 excel COM 插件中,我需要访问 pageSetup 属性。但是如果 excel 中的编辑模式处于活动状态,我会得到一个例外。

我可以使用以下代码检查编辑模式是否处于活动状态:

CommandBarControl oNewMenu = excelApp.CommandBars["Worksheet Menu Bar"].FindControl(
    1, //the type of item to look for
    18, //the item to look for
    refmissing, //the tag property (in this case missing)
    refmissing, //the visible property (in this case missing)
    true); //we want to look for it recursively so the last argument should be true.

    if ( oNewMenu != null )
    {
        // edit mode = true
        if (!oNewMenu.Enabled) {
        }
    }
Run Code Online (Sandbox Code Playgroud)

我找到了一些退出编辑模式的解决方案,但它们不起作用:

SendKeys.Flush();
excelApplication.SendKeys("{ENTER}");
Run Code Online (Sandbox Code Playgroud)

如何退出编辑模式以便我可以编写 pageSetup 属性?

Mat*_*rry 2

如果您使用的是外接程序,您可以尝试使用此退出编辑模式:

Globals.ThisAddIn.Application.SendKeys("{ENTER}");
Run Code Online (Sandbox Code Playgroud)

我建议使用与此类似的方法来确定 Excel 是否处于编辑模式:

public static void VerifyExcelIsNotInCellEditMode()
{
    if (Globals.ThisWorkbook.Application.Interactive)
    {
        try
        {
            //Will throw an error if user is editing cell
            Globals.ThisWorkbook.Application.Interactive = false;
            Globals.ThisWorkbook.Application.Interactive = true;
        }
        catch 
        {
            throw new Exception("Excel is in Edit Mode.");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)