我有一个名为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/