我已经为 Excel VSTO 项目编写了 Ribbon.xml 文件。选项卡元素如下所示:
<tab id="myId" idMso="TabAddIns" label="My Tab" visible="false">
打开工作簿时,我希望默认情况下隐藏该选项卡,这是通过将visible 属性设置为 false 来实现的。接下来,我想在 Workbook_Open 事件中将visible属性更改为true。这就是我被困住的地方。我不认为这会很难,但我花了几个小时在谷歌上搜索答案。似乎大多数示例 1)通过,这不是我想要做的,或者2)能够访问丝带的属性 https://stackoverflow.com/questions/6399111/vsto-enable-ribbon-button-only-when-a-document-is-loaded,到目前为止我还无法复制(尽管这些资源大部分都是旧的,所以我认为微软从那时起就移动了这些属性)。
有谁知道如何轻松地将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
}
}
功能区.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
}
}