【问题标题】:How to get excel application ribbon is hide/show using interop C#如何使用互操作 C# 隐藏/显示 excel 应用程序功能区
【发布时间】:2017-11-07 07:31:07
【问题描述】:

我正在 C# 中创建 Windows 应用程序,其中自动化 MsOffice Excel。

我正在尝试让 excel 应用程序功能区当前处于隐藏/显示状态。

以下代码:

public static bool EvaluateRibbonDisplay(IQuestion question, string filename, ShowHide display)
    {
        ExcelInterop.Application excelApplication = null;
        try
        {
            excelApplication = new ExcelInterop.Application() { Visible = false, DisplayAlerts = false, ScreenUpdating = false };
            ExcelInterop.Workbook excelWorkbook = excelApplication.Workbooks.Open(filename);

            bool result = false;
            var abc = excelApplication.ExecuteExcel4Macro("Get.ToolBar(7,\"Ribbon\")"); \\always returing true
            var abcd = excelApplication.CommandBars["Get.ToolBar(7,\"Ribbon\")"].Enabled;\\always returing true
            if (excelApplication.ExecuteExcel4Macro("Get.ToolBar(7,\"Ribbon\")") == Convert.ToBoolean(EnumStringExtension.GetDescription(display)))
                result = true;

            excelWorkbook.Close();

            return result;
        }
        catch (Exception)
        {
            return false;
        }
        finally
        {
            QuitApplication(excelApplication);
        }
    }

要隐藏/显示功能区,我使用 Ctrl+F1。但是上面的代码总是返回 true。

谁能帮帮我,我怎样才能获得功能区状态。

提前致谢。

【问题讨论】:

    标签: excel windows c#-4.0 office-interop


    【解决方案1】:

    对我来说,功能区中第一个控件的高度在隐藏时会发生变化。 Visible = 145, Hidden = 57。希望你能用这个观察做点什么

    Sub RibbonExample()
    
        Dim cbar As CommandBar
    
        For Each cbar In Application.CommandBars
            If cbar.Name = "Ribbon" Then
                Debug.Print cbar.Controls(1).Height & "x" & cbar.Controls(1).Width
            End If
        Next cbar
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2016-08-24
      • 2013-01-13
      • 2016-11-04
      • 1970-01-01
      • 1970-01-01
      • 2019-04-22
      • 2012-08-21
      • 1970-01-01
      • 2021-08-22
      相关资源
      最近更新 更多