Set visibility of tab based on workbook file opened on ribbon VSTO Excel

喜欢而已 提交于 2020-01-26 03:57:08

问题


I have a VSTO application and in the MainRibbon.xml I am using the getVisible handler to identify whether the tab should be visible or not. The method setVisbility is called in MainRibbon.cs. I am trying to set the visibility of the tab to true if the filename/workbook openend is called "Template.xlsm". Otherwise I dont want to show the below tab.

    <?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="Ribbon_Load" >
  <ribbon >
    <tabs>
      <tab idMso="TabAddIns" label="M Template" getVisible="setVisbility" >
        <group id="CreateLoadModel"
               label="Create/Load Model">
          <button id="createmodelbutton" label="Create New Model"
             screentip="Text" onAction="OnCreateModel"
             supertip="Create a new Model"
             imageMso="GroupSmartArtCreateGraphic"/>
        <button id="loadmodelbutton" label="Load Existing Model"
             screentip="Text" onAction="OnLoadModel"
             supertip="Load an Exisitng Model"
             imageMso="FileOpen"/>
        </group>





        public bool setVisbility(Office.IRibbonControl control)
    {

        var name = Globals.ThisAddIn.Application.ActiveWorkbook.FullName;

            if (Globals.ThisAddIn.Application.ActiveWorkbook != null &&
            Globals.ThisAddIn.Application.ActiveWorkbook.Name == "Template.xlsm")
            {
            return false;
            }

            else
            {
            return true;
            }


    }

Everything I currently run the application 'Globals.ThisAddIn.Application.ActiveWorkbook.Name ' is NULL object not defined. This is because the excel application opens without clicking on a file to open. However when i click on the necessary file - it goes back into this setVisibility method but it still states object not defined. How can I set the visibilitty to true/false based on the excel workbook I open?


回答1:


The problem occur because setVisibility is run before a workbook is loaded. That is setVisibility will run when the add-in is loaded.

One solution is to add a check for if any workbook is loaded by using Workbooks.Count and then update the visibility everytime a workbook is activated:

    public bool setVisbility(Office.IRibbonControl control)
    {
        int nWorkbooks = Globals.ThisAddIn.Application.Workbooks.Count;
        if (nWorkbooks == 0)
        {
            return false;
        }

        if (Globals.ThisAddIn.Application.ActiveWorkbook != null &&
        Globals.ThisAddIn.Application.ActiveWorkbook.Name == "Template.xlsm")
        {
            return true;
        }
        else
        {
            return false;
        }
    }

And then in your ThisAddIn_StartUp method add an eventhandler for activated workbook:

this.Application.WorkbookActivate += Application_WorkbookActivate;

Then when a workbook is activated force the ribbon to validate like:

    private void Application_WorkbookActivate(Workbook Wb)
    {
        RibbonClass.RibbonInstance.Invalidate()
    }

Depending on your naming of cource.



来源:https://stackoverflow.com/questions/59684605/set-visibility-of-tab-based-on-workbook-file-opened-on-ribbon-vsto-excel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!