无法显示或隐藏我的 VSTO Excel 加载项
Unable to Show or Hide my VSTO Excel 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 设置为 Custom
,则以下 c# 中的等效代码有效,但如果设置为 Office
,则无效(我假设你就是这种情况..)。所以在我看来,您在 VSTO 运行时中找到了一个 bug/limitation:只有在选项卡是自定义的(即,如果它位于新的独立选项卡上)时,才能更改可见性。
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
配置的选项卡,我们需要隐藏所有组。
正如 Malick 所说,这取决于功能区的 ControlIdType
如果将其更改为 Custom
,您在问题中发布的原始代码应该可以工作。
对于Office
,您必须隐藏所有组。隐藏所有组后,该选项卡将自动隐藏。
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
我有一条丝带叫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 设置为 Custom
,则以下 c# 中的等效代码有效,但如果设置为 Office
,则无效(我假设你就是这种情况..)。所以在我看来,您在 VSTO 运行时中找到了一个 bug/limitation:只有在选项卡是自定义的(即,如果它位于新的独立选项卡上)时,才能更改可见性。
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
配置的选项卡,我们需要隐藏所有组。
正如 Malick 所说,这取决于功能区的 ControlIdType
如果将其更改为 Custom
,您在问题中发布的原始代码应该可以工作。
对于Office
,您必须隐藏所有组。隐藏所有组后,该选项卡将自动隐藏。
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