C# 对于VSTO Word加载项:是否在另存为后激发事件?

C# 对于VSTO Word加载项:是否在另存为后激发事件?,c#,ms-word,event-handling,vsto,save-as,C#,Ms Word,Event Handling,Vsto,Save As,注意:尽管我的问题与我的问题大致相同,但目标和目的(以及由此产生的所需代码)是不同的。OP在美国: 文档保存到磁盘后,我需要捕获该事件, 关闭文件,执行我需要执行的操作,然后重新打开它 我的需求不同。看我的作品 注释结束 我有一个Word的VSTO插件,该插件设计用于操作RTF文件的各种元素(并且仅限于RTF文件)。外接程序由功能区按钮调用。如果用户打开一个RTF文档,然后执行另存为,我想捕获一个事件,以便检查为另存为选择的文件名,并禁用在扩展名不是.RTF时调用我的加载项的按钮 在我的ribb

注意:尽管我的问题与我的问题大致相同,但目标和目的(以及由此产生的所需代码)是不同的。OP在美国:

文档保存到磁盘后,我需要捕获该事件, 关闭文件,执行我需要执行的操作,然后重新打开它

我的需求不同。看我的作品

注释结束

我有一个Word的
VSTO
插件,该插件设计用于操作RTF文件的各种元素(并且仅限于RTF文件)。外接程序由功能区按钮调用。如果用户打开一个RTF文档,然后执行
另存为
,我想捕获一个事件,以便检查为另存为选择的文件名,并禁用在扩展名不是
.RTF
时调用我的加载项的按钮

在我的ribbon类ribbon load方法(在我的ribbon类的设计器文件中声明的事件处理方法:
this.load+=new Microsoft.Office.Tools.ribbon.RibbonIEventhandler(this.Ribbon1\u load)
)中,我对各种可用事件进行了编码(例如,
Globals.ThisAddIn.Application.DocumentChange+=Application\u DocumentChange;
Globals.ThisAddIn.Application.DocumentOpen+=Application\u DocumentOpen;
)但是所有可用的事件都是在
另存为
发生之前激发的,而不是之后激发的。我还在这个功能区加载方法中设置了一个断点。它在另存为之后不会再次执行(我并不感到惊讶)

我是否遗漏了什么?对于我的VSTO Word加载项,是否有在我的ribbon类中可捕获的
另存为
事件之后激发的事件,该事件将提供为
另存为
选择的文件名

根据Cindy Meister的答案更新我的代码

归功于Microsoft开发人员网络上的。我的代码源于

注意:我的VSTO功能区类名为
ClsLesCaveat
。它是一个新组,具有两个按钮,位于现有的
Insert
表中。它仅使用VSPRO 2017中的VSTO设计器创建

对我来说,我的功能区按钮需要在两种情况下禁用:

1) 如果有人使用没有.RTF扩展名的Word打开文件,则应禁用“我的功能区”按钮

2) 如果有人使用Word打开了一个.RTF文件(启用了“我的按钮”),但他们执行了另存为非.RTF文件的操作,则应禁用该非.RTF文档的“我的功能区”按钮

注意:不关心保存,因为“我的功能区”按钮在“打开”或“另存为”时处于启用/禁用状态

using System;
using System.IO;

namespace LesCaveatAddIn
{
    public partial class ThisAddIn
    {
        private bool allowSave = false;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {

            this.Application.DocumentBeforeSave += Application_DocumentBeforeSave;
            this.Application.DocumentOpen += Application_DocumentOpen;
        }

        # On open, disable buttons, enable buttons only if file extension is .RTF
        private void Application_DocumentOpen(Microsoft.Office.Interop.Word.Document Doc)
        {
            string extension = (Path.GetExtension(Doc.FullName)).ToUpper();

            Type type = typeof(ClsLesCaveat);
            ClsLesCaveat ribbon = Globals.Ribbons.GetRibbon(type) as ClsLesCaveat;

            ribbon.objButtonAddFouoCaveat.Enabled = false;
            ribbon.objButtonAddLesCaveat.Enabled = false;

            if (extension.Equals(".RTF"))
            {
                ribbon.objButtonAddFouoCaveat.Enabled = true;
                ribbon.objButtonAddLesCaveat.Enabled = true;
            }
        }

        # On save-as, handle the save-as myself. Cancel the save-as (since I just handled it). Then, disable buttons, enable buttons only if the save-as file extension is .RTF.
        private void Application_DocumentBeforeSave(Microsoft.Office.Interop.Word.Document Doc, ref bool SaveAsUI, ref bool Cancel)
        {
            if (!allowSave)
            {
                allowSave = true;

                if (SaveAsUI)
                {
                    // Display Save As dialog
                    Microsoft.Office.Interop.Word.Dialog d = Globals.ThisAddIn.Application.Dialogs[Microsoft.Office.Interop.Word.WdWordDialog.wdDialogFileSaveAs];
                    object timeOut = 0;
                    d.Show(ref timeOut);
                }
                else
                {
                    // Save without dialog
                    Doc.Save();
                }

                allowSave = false;
                Cancel = true;

                string extension = (Path.GetExtension(Doc.FullName)).ToUpper();

                Type type = typeof(ClsLesCaveat);
                ClsLesCaveat ribbon = Globals.Ribbons.GetRibbon(type) as ClsLesCaveat;

                ribbon.objButtonAddFouoCaveat.Enabled = false;
                ribbon.objButtonAddLesCaveat.Enabled = false;

                if (extension.Equals(".RTF"))
                {
                    ribbon.objButtonAddFouoCaveat.Enabled = true;
                    ribbon.objButtonAddLesCaveat.Enabled = true;
                }
            }
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        #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);
        }

        #endregion
    }
}
使用系统;
使用System.IO;
名称空间LescaveAttdin
{
公共部分类ThisAddIn
{
private bool allowSave=false;
私有void ThisAddIn_启动(对象发送方,System.EventArgs e)
{
this.Application.DocumentBeforeSave+=应用程序\u DocumentBeforeSave;
this.Application.DocumentOpen+=应用程序\u DocumentOpen;
}
#打开时,禁用按钮,仅当文件扩展名为.RTF时才启用按钮
专用作废应用程序\u DocumentOpen(Microsoft.Office.Interop.Word.Document文档)
{
字符串扩展=(Path.GetExtension(Doc.FullName)).ToUpper();
类型=类型(ClsLesCaveat);
ClsLesCaveat ribbon=Globals.Ribbons.GetRibbon(类型)作为ClsLesCaveat;
ribbon.objButtonAddFouoCaveat.Enabled=false;
ribbon.objbuttonadlescaveat.Enabled=false;
if(扩展名.Equals(“.RTF”))
{
ribbon.objButtonAddFouoCaveat.Enabled=true;
ribbon.objbuttonadlescaveat.Enabled=true;
}
}
#在“另存为”中,自己处理“另存为”。取消“另存为”(因为我刚刚处理过)。然后,禁用按钮,仅当“另存为”文件扩展名为.RTF时才启用按钮。
私有作废应用程序\u DocumentBeforeSave(Microsoft.Office.Interop.Word.Document文档,ref bool SaveAsUI,ref bool Cancel)
{
如果(!allowSave)
{
allowSave=true;
if(SaveAsUI)
{
//显示另存为对话框
Microsoft.Office.Interop.Word.Dialog d=Globals.ThisAddIn.Application.Dialogs[Microsoft.Office.Interop.Word.WdWordDialog.wdDialogFileSaveAs];
对象超时=0;
d、 显示(参考超时);
}
其他的
{
//无对话框保存
Doc.Save();
}
allowSave=false;
取消=真;
字符串扩展=(Path.GetExtension(Doc.FullName)).ToUpper();
类型=类型(ClsLesCaveat);
ClsLesCaveat ribbon=Globals.Ribbons.GetRibbon(类型)作为ClsLesCaveat;
ribbon.objButtonAddFouoCaveat.Enabled=false;
ribbon.objbuttonadlescaveat.Enabled=false;
if(扩展名.Equals(“.RTF”))
{
ribbon.objButtonAddFouoCaveat.Enabled=true;
ribbon.objbuttonadlescaveat.Enabled=true;
}
}
}
私有void ThisAddIn_关闭(对象发送方,System.EventArgs e)
{
}
#区域VSTO生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InternalStartup()
{
this.Startup+=new System.EventHandler(ThisAddIn\u启动);
this.Shutdown+=new System.EventHandler(ThisAddIn\u Shutdown);
}
#端区
}
}

否,没有捕获任何保存或操作后保存的事件。唯一与储蓄有关的是

DocumentBeforeSave提供的参数允许开发人员抑制内置UI(SaveAs对话框)以及取消触发
    private void ThisDocument_BeforeSave(object sender, object e)
    {
        //Suppress the built-in SaveAs interface (dialog box)
        e.SaveAsUi = false;
        //Cancel the default action
        e.Cancel = true;
        Word.Dialog dlg = wdApplication.Dialogs[Microsoft.Office.Interop.Word.WdWordDialog.wdDialogFileSaveAs];
        //Word dialog box parameters have to be accessed via Late-Binding (PInvoke) 
        //To get the path, use the Name property
        object oDlg = (object)dlg;
        object[] oArgs = new object[1];
        oArgs[0] = (object)@"";
        dlg.Show(ref missing);
        object fileName = oDlg.GetType().InvokeMember("Name", BindingFlags.GetProperty, null, oDlg, oArgs);
    }