首页 > 解决方案 > 当我从 Web 打开 Excel 文档时,无法重新调整 Excel 的 ctrl+s 事件的用途

问题描述

我使用以下代码从 web url 打开一个 excel 文档:

<a href="ms-excel:ofe|u|https://www.example.com/test.xlsx">Open with excel</a>

一个空的 excel 书被成功打开。根据我的 excel vsto 项目的场景,文档必须保存在我们的云中,而不是用户的电脑中。因此,我重新利用了 excel 的保存事件,创建了一个名为 CustomSave 的方法,并在其中编写了我的上传代码。

我的功能区和 c# 代码如下所示:

MainRibbon.xml

 <commands>
    <command idMso="ThemeSaveCurrent" onAction="CustomSave"/>
    <command idMso="FileSaveAsExcelXlsb" onAction="CustomSave"/>
    <command idMso="ChartSaveTemplates" onAction="CustomSave"/>
    <command idMso="WindowSaveWorkspace" onAction="CustomSave"/>
    <command idMso="FileSaveACopy" onAction="CustomSave"/>
    <command idMso="FileSave" onAction="CustomSave" />
    <command idMso="FileSaveAs" onAction="CustomSave" />
    <command idMso="FileSaveAsMenu" onAction="CustomSave" />
    <command idMso="FileSaveAsExcelXlsx" onAction="CustomSave" />
    <command idMso="FileSaveAsExcelXlsb" onAction="CustomSave"  />
    <command idMso="FileSaveAsExcel97_2003" onAction="CustomSave" />
    <command idMso="FileSaveAsOtherFormats" onAction="CustomSave"/>
  </commands>

MainRibbon.cs

public void CustomSave(IRibbonControl control, bool cancelDefault)
{              
    MyUploadMethod(); //Method which uploads current document to the cloud.
    cancelDefault = false;
}

我还添加了 WorkbookBeforeSave 方法:

ThisAddIn.cs

private void WorkbookBeforeSave(Excel.Workbook workbook, bool SaveAsUI,ref bool Cancel)
{
    Cancel = true;
    SaveAsUI = false;
    MyUploadMethod(); //Method which uploads current document to the cloud.

}

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    this.Application.WorkbookBeforeSave += WorkbookBeforeSave;

}

尽管我的 CustomSave 方法按预期拦截了 excel 中的所有保存事件,但是当我从键盘按 ctrl+s 时,它不会进入 CustomSave 方法或 WorkbookBeforeSave 方法。当我单击 ctrl+s 键时,我收到以下警告:

点击图片

我对 word 和 power point 做了同样的事情,没有问题,但对 excel 却做不到。顺便说一句,当我从 Web 打开文档时,文档状态始终变为只读,除非我将其保存在本地,否则它的状态不会改变。如果不保存在本地,我无法将文档直接上传到云端。但我不想将文档保存到本地电脑。如果您对我的问题有任何建议,我会很高兴。

标签: c#excelvstooffice-addinsribbonx

解决方案


推荐阅读