无法在用户操作和Excel处理标准按钮点击事件之间挂钩

发布于 2025-02-03 21:16:59 字数 3707 浏览 4 评论 0 原文

需要什么: 我需要一个Excel Addin,可以捕获功能区选项卡上各种标准命令栏按钮的点击事件。另外,完成此操作后,应进行命令按钮的正常处理。

示例: 当我捕获功能区按钮点击事件时,单击“保存”按钮时,我应该能够 a)进行自定义处理,显示消息框或记录此单击事件到平面文件 工作表的实际工作

b)让Excel做保存我推荐的

- 在Office Fluent Ribbon上暂时重新调整命令

自定义开发人员的2007年Office Fluent Ribbon

,尽管可以捕获标准按钮点击am 无法获得两个点a)和b)同时工作。

ribbon1.xml

<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
  <commands>
      <command idMso="FileSave" onAction="OnRibbonButtonClick" />
  </commands>
</customUI>

ribbon1.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using Office = Microsoft.Office.Core;

namespace ExcelAddIn
{
    [ComVisible(true)]
    public class Ribbon1 : Office.IRibbonExtensibility
    {
        private Office.IRibbonUI ribbon;

        public Ribbon1()
        {
        }

        #region IRibbonExtensibility Members

        public string GetCustomUI(string ribbonID)
        {
            return GetResourceText("ExcelAddIn.Ribbon1.xml");
        }

        #endregion

        #region Ribbon Callbacks
        //Create callback methods here. For more information about adding callback methods, visit https://go.microsoft.com/fwlink/?LinkID=271226

        public void Ribbon_Load(Office.IRibbonUI ribbonUI)
        {
            this.ribbon = ribbonUI;
        }

        public void OnRibbonButtonClick(Office.IRibbonControl control, bool Cancel)
        //public void OnRibbonButtonClick(Office.IRibbonControl control)
        {
            Globals.ThisAddIn.OnStandardRibbonCommand(control.Id);
            ribbon.Invalidate();
            Cancel = false;
        }           
        #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
    }
}

带有上述源代码,我可以捕获FileSave事件,但该文件无法保存。即不会发生保存文件的默认excel功能。

我缺少某件事还是无法做到

这是否意味着命令已完全重新使用并且无法实现?

环境: Microsoft®Excel®Microsoft365 MSO(版本2205 Build 16.0.15225.20172)64位

Microsoft Visual Studio Enterprise 2019版本16.11.11.7

VSTO版本10.0.60724

What is needed:
I need an Excel addin which can capture the click events of various Standard Command bar buttons on the Ribbon tabs. Also, once this is done the normal processing of the command button should proceed.

Example :
When I capture ribbon button click event, when the "Save" button is clicked , I should be able to
a) Do custom processing, display a message box or log this click event to a flat file AND
b) Let excel do the actual work of saving the worksheet

I have referred -

Temporarily Repurpose Commands on the Office Fluent Ribbon

Customizing the 2007 Office Fluent Ribbon for Developers

Although, am able to capture the standard button clicks am not able to get both points a) and b) to work at the same time.

Ribbon1.xml

<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
  <commands>
      <command idMso="FileSave" onAction="OnRibbonButtonClick" />
  </commands>
</customUI>

Ribbon1.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using Office = Microsoft.Office.Core;

namespace ExcelAddIn
{
    [ComVisible(true)]
    public class Ribbon1 : Office.IRibbonExtensibility
    {
        private Office.IRibbonUI ribbon;

        public Ribbon1()
        {
        }

        #region IRibbonExtensibility Members

        public string GetCustomUI(string ribbonID)
        {
            return GetResourceText("ExcelAddIn.Ribbon1.xml");
        }

        #endregion

        #region Ribbon Callbacks
        //Create callback methods here. For more information about adding callback methods, visit https://go.microsoft.com/fwlink/?LinkID=271226

        public void Ribbon_Load(Office.IRibbonUI ribbonUI)
        {
            this.ribbon = ribbonUI;
        }

        public void OnRibbonButtonClick(Office.IRibbonControl control, bool Cancel)
        //public void OnRibbonButtonClick(Office.IRibbonControl control)
        {
            Globals.ThisAddIn.OnStandardRibbonCommand(control.Id);
            ribbon.Invalidate();
            Cancel = false;
        }           
        #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
    }
}

With the above source code, I can capture the FileSave event, but the file does not get saved. i.e. the default Excel funtionality of saving the file does not happen.

Am I missing something or this cannot be done?

[Temporarily Repurpose Commands on the Office Fluent Ribbon]
Does this mean that the command is completely repurposed and the original purpose cannot be fulfilled?

Environment:
Microsoft® Excel® for Microsoft 365 MSO (Version 2205 Build 16.0.15225.20172) 64-bit

Microsoft Visual Studio Enterprise 2019 Version 16.11.7

VSTO version 10.0.60724

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十年九夏 2025-02-10 21:16:59

重新使用的回调应具有以下签名:

C#: void OnAction(IRibbonControl control, ref bool CancelDefault)

VBA: Sub OnAction(control As IRibbonControl, byRef CancelDefault)

C++: HRESULT OnAction([in] IRibbonControl *pControl, [in,out] VARIANT _BOOL *fCancelDefault)

Visual Basic: Sub OnAction(control As IRibbonControl, byRef CancelDefault)

似乎您需要使用以下方法:

public void OnRibbonButtonClick(Office.IRibbonControl control, ref bool Cancel)
{
    Debug.Print(control.Id);
    Cancel = false;
}   

The repurposed callback should have the following signature:

C#: void OnAction(IRibbonControl control, ref bool CancelDefault)

VBA: Sub OnAction(control As IRibbonControl, byRef CancelDefault)

C++: HRESULT OnAction([in] IRibbonControl *pControl, [in,out] VARIANT _BOOL *fCancelDefault)

Visual Basic: Sub OnAction(control As IRibbonControl, byRef CancelDefault)

It seems you need to use the following method instead:

public void OnRibbonButtonClick(Office.IRibbonControl control, ref bool Cancel)
{
    Debug.Print(control.Id);
    Cancel = false;
}   
仙女山的月亮 2025-02-10 21:16:59

处理重新启动按钮点击事件后,您只需调用 worksheet.save 即可。

或者,您可以避免重新启动,只需处理 WordSheet.beforesave / afterSave 事件即可。

After you process the repurposed button click event, you can simply call Worksheet.Save.

Or you can avoid repurposing and just handle Wordsheet.BeforeSave/AfterSave events.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文