c# - 无法显示或隐藏我的 VSTO Excel 加载项

标签 c# excel vb.net vsto add-in

我有一个名为Ribbon 1的功能区

我希望仅在工作簿打开时显示功能区。如果只有 Excel 应用程序正在运行并且没有打开工作簿,那么我想隐藏我的功能区选项卡。我怎样才能做到这一点?

这是我尝试过的,但它没有隐藏功能区

Public Class ThisAddIn
    Private Sub Application_WorkbookOpen(ByVal doc As Excel.Workbook) Handles Application.WorkbookOpen
        If Application.Workbooks.Count > 0 Then
            If Globals.Ribbons.Ribbon1.Tab1.Visible = False Then Globals.Ribbons.Ribbon1.Tab1.Visible = True
        End If
    End Sub

    Private Sub Application_WorkbookBeforeClose(ByVal doc As Excel.Workbook, ByRef Cancel As Boolean) Handles Application.WorkbookBeforeClose
        If Application.Workbooks.Count = 1 Then
            If Globals.Ribbons.Ribbon1.Tab1.Visible = True Then Globals.Ribbons.Ribbon1.Tab1.Visible = False
        End If
    End Sub
End Class

我没有收到任何错误。它根本就没有隐藏它。我在 If Globals.Ribbons.Ribbon1.Tab1.Visible = True then Globals.Ribbons.Ribbon1.Tab1.Visible = False 上放置了一个断点。该行已执行,但选项卡未隐藏。我脑子都快结冰了!这是做我想做的事情的正确方法吗?

最佳答案

如果 ControlIdType 则以下 C# 中的等效代码可以工作功能区的设置为 Custom,但是如果设置为 Office 则不起作用(我认为您就是这种情况..)。因此,在我看来,您在 VSTO 运行时中发现了一个错误/限制:只有当选项卡是自定义的(即,如果它位于新的独立选项卡上)时,才可以更改可见性。

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        this.Application.WorkbookBeforeClose += Application_WorkbookBeforeClose;
        this.Application.WorkbookOpen += Application_WorkbookOpen;
    }

    private void Application_WorkbookOpen(Excel.Workbook Wb)
    {
        if (this.Application.Workbooks.Count > 0) {
            if (Globals.Ribbons.Ribbon1.tab1.Visible == false) Globals.Ribbons.Ribbon1.tab1.Visible = true;
        }

    }

    private void Application_WorkbookBeforeClose(Excel.Workbook Wb, ref bool Cancel)
    {
        if (this.Application.Workbooks.Count == 1)
        {
            if (Globals.Ribbons.Ribbon1.tab1.Visible == true) Globals.Ribbons.Ribbon1.tab1.Visible = false;
        }
    }

编辑:正如 Siddharth Rout 的回答中所正确显示的,这不是一个错误:要隐藏具有 Office 配置的选项卡,我们需要隐藏所有组。

关于c# - 无法显示或隐藏我的 VSTO Excel 加载项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60462342/

相关文章:

excel,从1个表中获取数字,并根据第二个标准求和

c# - 为什么在集合中找不到 Mysql 存储过程参数?

c# - 将字符串转换为日期时间 UTC

c# - 错误 : 'Subscript indices must either be real positive integers or logicals' when using Matlab . NET 生成器

excel - 重新加载表单时如何保留文本框中的值

xml - VBA Excel SelectSingleNode 语法

vb.net - 从excel导入数据并合并

.net - 从 VB.NET 中的 FTP 文件夹下载所有文件和子目录

asp.net - 将 ASP.NET 部署到 Windows Azure 云,应用程序在云上运行时出现错误

c# - 将 UNIX 时间格式的 UTC 时间转换为可读的 DateTime 格式?