【问题标题】:Excel vsto end edit modeExcel vsto 结束编辑模式
【发布时间】:2014-04-04 14:56:55
【问题描述】:

在 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) {
        }
    }

我找到了一些退出编辑模式的解决方案,但没有奏效:

SendKeys.Flush();
excelApplication.SendKeys("{ENTER}");

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

【问题讨论】:

  • 据 Microsoft Office 可扩展性团队的项目经理 this answer,目前不支持此功能。

标签: .net excel com vsto


【解决方案1】:

如果您正在使用插件,您可以尝试使用它来退出编辑模式:

Globals.ThisAddIn.Application.SendKeys("{ENTER}");

我会推荐与此方法类似的方法来确定 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.");
        }
    }
}

【讨论】:

  • 这种使用 Interactive 属性检查编辑模式的方法效果很好,但是带有 {ENTER} 或 {ESCAPE} 的 SendKeys,无论有无 SendKeys.Flush 或 Application.DoEvents 似乎都不起作用Excel 退出单元格编辑模式。
  • 我也有同样的问题。 SendKeys 对 Excel 无效。知道如何解决这个问题吗?
猜你喜欢
  • 2012-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多