【发布时间】:2018-11-30 18:23:45
【问题描述】:
我已经为 Excel VSTO 项目编写了 Ribbon.xml 文件。选项卡元素如下所示:
<tab id="myId" idMso="TabAddIns" label="My Tab" visible="false">
当打开工作簿时,我希望默认情况下隐藏选项卡,这是通过将 visible 属性设置为 false 来实现的。接下来,我想在 Workbook_Open 事件中将 visible 属性更改为 true。这就是我卡住的地方。我认为这并不难,但我花了几个小时在谷歌上搜索答案。似乎大多数示例1)通过button callback切换选项卡的可见性,这不是我想要做的,或者2)能够访问ribbon's properties,到目前为止我还无法复制(尽管,这些资源大部分都是旧的,所以我认为 MS 从那时起移动了这些属性)。
有谁知道如何轻松地将 visible 属性更改为 true 以便显示选项卡?
谢谢!
更新了更多信息:
ThisAddIn.cs
namespace Doodles_Reporting
{
public partial class ThisAddIn
{
public RibbonApi ribbonApi;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
{
return new Ribbon();
}
void Application_WorkbookOpen(Excel.Workbook Wb)
{
//first, check if there is an application/process for each workbook
Excel.Workbooks books = Globals.ThisAddIn.Application.Workbooks;
if (books.Count > 1)
{
try
{
//close workbook that was just opened and then reopen it with new process/application.
string filePath = Wb.FullName;
Wb.Close();
Excel.Application excelApp = new Excel.Application();
excelApp.Visible = true;
excelApp.DisplayFullScreen = true;
excelApp.Workbooks.Open(filePath);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK);
}
}
else
{
//second, check if the workbook is a Doodles workbook
try
{
DocumentProperties props = (DocumentProperties)Wb.CustomDocumentProperties;
var selectedTable = props["selectedTable"].Value;
configureDoodles();
}
catch (Exception)
{
//THIS IS WHERE I WANT TO SET THE RIBBON VISIBILITY TO FALSE
}
}
}
private void configureDoodles()
{
RibbonApi.app = Globals.ThisAddIn.Application;
RibbonApi.wBookPropertiesConfig = new WorkbookPropertiesConfig(RibbonApi.app.ActiveWorkbook);
RibbonApi.presenter = new ExcelPresenter(RibbonApi.app.ActiveWorkbook);
ribbonApi = new RibbonApi();
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
this.Application.WorkbookOpen += new Excel.AppEvents_WorkbookOpenEventHandler(Application_WorkbookOpen);
}
#endregion
}
}
Ribbon.cs
namespace Doodles_Reporting
{
[ComVisible(true)]
public class Ribbon : Office.IRibbonExtensibility
{
private Office.IRibbonUI ribbon;
public Ribbon()
{
}
#region IRibbonExtensibility Members
public string GetCustomUI(string ribbonID)
{
return GetResourceText("Doodles_Reporting.Ribbon.xml");
}
#endregion
#region Ribbon Callbacks
//Create callback methods here. For more information about adding callback methods, visit http://go.microsoft.com/fwlink/?LinkID=271226
public void Ribbon_Load(Office.IRibbonUI ribbonUI)
{
this.ribbon = ribbonUI;
}
public bool toggleVisibility(Office.IRibbonControl control)
{
return (control.Id == "TabAddIns") ? true : false;
}
public void onSomeEvent()
{
this.ribbon.InvalidateControl("TabAddIns");
}
public void SignIn(Office.IRibbonControl ribbonUI)
{
Globals.ThisAddIn.ribbonApi.signIn();
}
public void SqlCreatorFormLoad(Office.IRibbonControl ribbonUI)
{
Globals.ThisAddIn.ribbonApi.showSqlCreator();
}
public void refreshData(Office.IRibbonControl ribbonUI)
{
Globals.ThisAddIn.ribbonApi.refreshData();
}
public void drilldownSelectionLoad(Office.IRibbonControl ribbonUI)
{
Globals.ThisAddIn.ribbonApi.setDrilldownColumns();
}
public void Drilldown(Office.IRibbonControl ribbonUI)
{
Globals.ThisAddIn.ribbonApi.drilldown();
}
public void editProperties(Office.IRibbonControl ribbonUI)
{
}
#endregion
#region Helpers
private static string GetResourceText(string resourceName)
{
Assembly asm = Assembly.GetExecutingAssembly();
string[] resourceNames = asm.GetManifestResourceNames();
for (int i = 0; i < resourceNames.Length; ++i)
{
if (string.Compare(resourceName, resourceNames[i], StringComparison.OrdinalIgnoreCase) == 0)
{
using (StreamReader resourceReader = new StreamReader(asm.GetManifestResourceStream(resourceNames[i])))
{
if (resourceReader != null)
{
return resourceReader.ReadToEnd();
}
}
}
}
return null;
}
#endregion
}
}
【问题讨论】:
-
您尝试了哪些方法,哪些方法不起作用?是您无法从 Workbook_Open 处理程序中获取对选项卡的引用(使用静态字段),还是不起作用?
-
对,我无法从 Workbook_Open 处理程序中获得对选项卡的引用。请原谅我无知的问题,但是静态字段应该是什么?我也看到了这个链接 (msdn.microsoft.com/en-us/library/bb772088.aspx),但我无法在 Globals.Ribbons 下找到我的功能区名称。
-
哪个先触发?
Application_WorkbookOpen或CreateRibbonExtensibilityObject? -
创建RibbonExtensibilityObject
-
发布了另一个答案,但我意识到更重要的问题是:哪个先触发 -
Application_WorkbookOpen或Ribbon_Load?