首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >是否有办法在Excel中禁用或限制对功能区/菜单栏的访问?

是否有办法在Excel中禁用或限制对功能区/菜单栏的访问?
EN

Stack Overflow用户
提问于 2019-02-18 19:51:13
回答 1查看 679关注 0票数 0

我想打开一个非模式的Win窗体,并想限制对功能区/菜单栏的访问。基本上,我只想提供对Excel单元格和工作表的访问。

在调用窗体之前,我已经尝试了下面的代码,但它不起作用。

代码语言:javascript
运行
复制
        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;
            }
        }
EN

Stack Overflow用户

发布于 2019-02-19 07:27:11

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

视频

XML

代码语言:javascript
运行
复制
<?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

代码语言:javascript
运行
复制
/// <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();
}
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54746687

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档