通过Workbook_Open事件更改VSTO选项卡的可见属性 [英] Changing the VSTO tab's visible property through the Workbook_Open event

查看:44
本文介绍了通过Workbook_Open事件更改VSTO选项卡的可见属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为Excel VSTO项目编写了Ribbon.xml文件.tab元素如下所示:

I have written the Ribbon.xml file for an Excel VSTO project. The tab element looks like this:

<tab id="myId" idMso="TabAddIns" label="My Tab" visible="false">

打开工作簿时,我希望默认情况下隐藏选项卡,这是通过将visible属性设置为false来完成的.接下来,我想在Workbook_Open事件中将visible属性更改为true.这就是我卡住的地方.我认为这不会很困难,但是我花了几个小时的时间来寻找答案.似乎大多数示例1)通过功能区的属性,我无法复制,因此远(尽管这些资源大多数都是旧资源,所以我认为MS从那时起便将这些属性转移了.)

When a workbook is opened, I want the tab to be hidden by default, which is accomplished through the visible property being set to false. Next, I want to change the visible property to true in the Workbook_Open event. This is where I'm stuck. I wouldn't think this would be hard, but I've spent a couple hours googling for the answer. It seems that most examples 1) toggle a tab's visibility through a button callback, which is not what I want to do, or 2) are able to access the ribbon's properties, which I have not been able to replicate so far (although, most of these resources are old, so I'm thinking that MS moved these properties around since then).

有人知道如何轻松地将visible属性更改为true以便显示选项卡吗?

Does anyone know how to easily change the visible property to true so that the tab is displayed?

谢谢!

已更新其他信息:

ThisAddIn.cs

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

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
    }
}

推荐答案

对于任何想知道如何使用设计人员提供的功能区进行操作的人:

For anyone wondering how to do this with a ribbon from designer:

如果您尝试过此操作:

Globals.Ribbons.MyRibbon.MyRibbonTab.Visible = false;

什么也没有发生,请尝试将所有功能区组设置为Visible = false.当功能区中的所有功能区组都不可见时,整个功能区将自动设置为Visible = false.

And nothing happened, try setting all your RibbonGroups to Visible = false. When all RibbonGroups inside your Ribbon are Not visible, the whole ribbon will automaticly set to Visible = false.

private void SetRibbonVisibility(bool visible)
    {
        foreach (var ribbonGroup in Globals.Ribbons.Ribbon.MyRibbonTab.Groups)
        {
            ribbonGroup.Visible = visible;
        }
    }

是的,与VSTO合作很愉快.

Yes, VSTO is a joy to work with.

这篇关于通过Workbook_Open事件更改VSTO选项卡的可见属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆