首页 > 解决方案 > 有没有办法在 Excel 中禁用或限制对功能区/菜单栏的访问?

问题描述

我想打开一个非模态 Win Form 并想限制对功能区/菜单栏的访问。基本上,我只想授予对 Excel 单元格和表格的访问权限。

我在调用表单之前尝试了以下代码,但它不起作用。

        for (int i = 1; i < Globals.ThisAddIn.Application.CommandBars.Count; i++)
        {
            for (int c = 1; c < Globals.ThisAddIn.Application.CommandBars[i].Controls.Count; c++)
            {
                Globals.ThisAddIn.Application.CommandBars[i].Controls[c].Enabled = false;
            }
        }

标签: c#excelvsto

解决方案


您可以使用以下 XML/C# 按名称显示/隐藏功能区。我使用了一个设置来保存可见性的值。

环境

视频

gif

XML

<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
    <ribbon>
        <tabs>
            <tab idMso="TabHome" getVisible="GetVisible" />
            <tab idMso="TabInsert" getVisible="GetVisible" />
            <tab idMso="TabReview" getVisible="GetVisible" />
            <tab idMso="TabData" getVisible="GetVisible" />
            <tab idMso="TabView" getVisible="GetVisible" />
            <tab idMso="TabFormulas" getVisible="GetVisible" />
            <tab idMso="TabPageLayoutExcel" getVisible="GetVisible" />
            <tab idMso="TabDeveloper" getVisible="GetVisible" />
            <tab idMso="TabPrintPreview" getVisible="GetVisible" />
            <tab idMso="TabAddIns" getVisible="GetVisible" />
            <tab idMso="TabSetTableToolsExcel" getVisible="GetVisible" />
            <tab
                    id="tabExample"
                    label="Example"
                    insertAfterMso="TabHome"
                    keytip="EX"
                    >
                <group
                        id="grpRibbonVisibility"
                        label="Ribbon Visibility"
                        imageMso="WatchWindow"
                        >
                    <toggleButton
                        id="tglShowHideRibbons"
                        label="Show Hide Ribbons"
                        getPressed="GetPressed"
                        onAction="OnAction_Boolean"
                        imageMso="WatchWindow"
                        size="large"
                        screentip="Show or Hide Ribbons"
                        supertip="This will show or hide the system ribbons."
                        keytip="SHR"
                        />
                </group>
            </tab>
        </tabs>
    </ribbon>
</customUI>

C#

/// <summary> 
/// Assigns the visiblity to controls
/// </summary>
/// <param name="control">Represents the object passed into the callback procedure of a control in a ribbon or another user interface that can be customized by using Office Fluent ribbon extensibility. </param>
/// <returns>A method that returns true or false if the control is visible </returns> 
public bool GetVisible(Office.IRibbonControl control)
{
    try
    {
        switch (control.Id)
        {
            case "TabHome":
            case "TabInsert":
            case "TabReview":
            case "TabData":
            case "TabView":
            case "TabFormulas":
            case "TabPageLayoutExcel":
            case "TabDeveloper":
            case "TabPrintPreview":
            case "TabAddIns":
            case "TabSetTableToolsExcel":
                return Properties.Settings.Default.IsRibbonVisible;
            default:
                return false;
        }
    }
    catch (Exception ex)
    {
        //ErrorHandler.DisplayMessage(ex);
        return false;
    }
}

/// <summary>
/// Used for boolean controls like checkboxes and toggle buttons
/// </summary>
/// <param name="control"></param>
/// <param name="pressed"></param>
public void OnAction_Boolean(Office.IRibbonControl control, bool pressed)
{
    try
    {
        switch (control.Id)
        {
            case "tglShowHideRibbons":
                Properties.Settings.Default.IsRibbonVisible = pressed;
                break;
        }
        ribbon.Invalidate();

    }
    catch (Exception)
    {
        //ErrorHandler.DisplayMessage(ex);
    }

}

/// <summary>
/// To return the current value for boolean controls
/// </summary>
/// <param name="control"></param>
/// <returns></returns>
public bool GetPressed(Office.IRibbonControl control)
{
    try
    {
        switch (control.Id)
        {
            case "tglShowHideRibbons":
                return Properties.Settings.Default.IsRibbonVisible;
            default:
                return true;
        }

    }
    catch (Exception)
    {
        //ErrorHandler.DisplayMessage(ex);
        return true;
    }


}

/// <summary>
/// Used to update/reset the ribbon values
/// </summary>
public void InvalidateRibbon()
{
    ribbon.Invalidate();
}

推荐阅读