[英]Unable to Show or Hide my VSTO Excel Add-In
I have a ribbon called Ribbon 1
我有一条叫做
Ribbon 1
I want my ribbon to show only when a workbook is open.我希望我的功能区仅在工作簿打开时显示。 If only the excel application is running and no workbooks are open then I want to hide my ribbon tab.
如果只有 excel 应用程序正在运行并且没有打开工作簿,那么我想隐藏我的功能区选项卡。 How can I do that?
我怎样才能做到这一点?
This is what I tried but it is not hiding the ribbon这是我尝试过的,但它并没有隐藏功能区
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
I am not getting any error.我没有收到任何错误。 It is simply not hiding it.
它只是没有隐藏它。 I put a break point on
If Globals.Ribbons.Ribbon1.Tab1.Visible = True Then Globals.Ribbons.Ribbon1.Tab1.Visible = False
.我在
If Globals.Ribbons.Ribbon1.Tab1.Visible = True Then Globals.Ribbons.Ribbon1.Tab1.Visible = False
上设置了一个断点。 The line executed but the tab didn't hide.该行执行但选项卡没有隐藏。 I am having a brain freeze!
我脑子被冻住了! Is this the right way to do what I want?
这是做我想做的正确方法吗?
The following equivalent code in c# works if the ControlIdType of the Ribbon is set to Custom
, however it doesn't work if it is set to Office
(I assume it is the case for you..).如果 Ribbon 的ControlIdType设置为
Custom
,则 c# 中的以下等效代码有效,但是如果设置为Office
则无效(我假设您就是这种情况......)。 So it seems to me that you find a bug/limitation in the VSTO runtime: it is only possible to change the visibility if the tab is custom (ie if it is on a new independent tab).因此,在我看来,您在 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;
}
}
Edit: As properly shown in Siddharth Rout's answer, it is not a bug: to hide a tab with the Office
configuration, we need to hide all the groups.编辑:正如 Siddharth Rout 的回答中正确显示的那样,这不是错误:要隐藏带有
Office
配置的选项卡,我们需要隐藏所有组。
As Malick mentioned, it depends on the ControlIdType
of the Ribbon正如 Malick 提到的,它取决于功能区的
ControlIdType
If you change it to Custom
, your original code posted in the question should work.如果您将其更改为
Custom
,则您在问题中发布的原始代码应该可以工作。
For Office
you have to hide all the groups.对于
Office
您必须隐藏所有组。 Once all the Groups are hidden, the tab will automatically hide.隐藏所有组后,该选项卡将自动隐藏。
Public Class ThisAddIn
'~~> Workbook Open
Private Sub Application_WorkbookOpen(ByVal doc As Excel.Workbook) Handles Application.WorkbookOpen
If Application.Workbooks.Count > 0 Then
For Each ribbonGroup In Globals.Ribbons.Ribbon1.Tab1.Groups
ribbonGroup.Visible = True
Next
End If
End Sub
'~~> This is if the user presses CTRL + N for a new workbook
Private Sub Application_WorkbookActivate(ByVal doc As Excel.Workbook) Handles Application.WorkbookActivate
If Application.Workbooks.Count > 0 Then
For Each ribbonGroup In Globals.Ribbons.Ribbon1.Tab1.Groups
ribbonGroup.Visible = True
Next
End If
End Sub
'~~> Before Close
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
For Each ribbonGroup In Globals.Ribbons.Ribbon1.Tab1.Groups
ribbonGroup.Visible = False
Next
End If
End If
End Sub
End Class
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.