注意:虽然我的问题与VSTO Word post保存事件大致相同,但目的和目标(以及由此产生的所需代码)是不同的。VSTO Word post保存事件中的OP声明:
将文档保存到磁盘后,我需要捕获该事件,关闭文件,做我需要做的事情并重新打开它。
我的需求不一样。看我的行动。
of Note
我有一个VSTO外接程序Word,它被设计用来操纵RTF文件的各种元素(而且只有RTF文件)。外接程序由带状按钮调用。如果用户打开一个RTF文档,然后执行save-as,我想要捕获一个事件,这样我就可以检查为save选择的文件名,并禁用在扩展名不是.RTF的情况下调用我的外接程序的按钮。
在我的带状类带状加载方法(在我的带状类的设计器文件:this.Load += new Microsoft.Office.Tools.Ribbon.RibbonUIEventHandler(this.Ribbon1_Load)中断言的事件处理方法)中,我已经编码了各种可用的事件(例如,Globals.ThisAddIn.Application.DocumentChange += Application_DocumentChange;和Globals.ThisAddIn.Application.DocumentOpen += Application_DocumentOpen;),但是所有可用的事件都是在save-as发生之前触发的,而不是在save-as发生之后。我还在这个带状加载方法中放置了一个断点。它不会在保存后再次执行(我并不感到惊讶)
我是不是遗漏了什么?对于VSTO Word外接程序,是否有在save-as事件之后触发的事件,该事件在我的带类中是可捕获的,它将提供为save-as选择的文件的名称。
更新我的代码反映辛迪·迈斯特的答案
归功于微软开发者网络上的约瑟夫·福克斯。我的代码来自文档保存事件
注意:我的VSTO带状类名为ClsLesCaveat。它是一个新的组,在现有的Insert表中有两个按钮。它是在VS Pro 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
}
}发布于 2019-03-12 21:50:29
不,在操作后没有捕获任何保存或保存的事件。唯一与保存有关的是DocumentBeforeSave。
DocumentBeforeSave确实提供了参数,使开发人员能够抑制内置UI (SaveAs对话框),并取消触发事件的操作。这允许开发人员提供自己的接口来保存(as),从而能够确定文档何时保存(as),并根据文件名、扩展名或其他条件采取任何所需的操作。
还可以在C#中使用Word内置的SaveAs对话框,而不是创建自己的对话框,因为它需要使用PInvoke。下面是一个例子,让您知道这是如何工作的(在移动设备上没有经过测试):
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);
}可以使用的可用对话框参数列出了这里。
https://stackoverflow.com/questions/55128187
复制相似问题