Ms word msword中的被动扩展

Ms word msword中的被动扩展,ms-word,vsto,system.reactive,Ms Word,Vsto,System.reactive,我想知道是否有可能在Word中使用被动扩展。我看过Jeff演示如何在excel中连接工作簿打开事件 我想知道我是否能在word中做同样的事情 我已经走了这么远 public static class ApplicationExtensions { public static IObservable<Word.Document> DocumentBeforeSaveAsObservable(this Word.Application application)

我想知道是否有可能在Word中使用被动扩展。我看过Jeff演示如何在excel中连接工作簿打开事件

我想知道我是否能在word中做同样的事情

我已经走了这么远

     public static class ApplicationExtensions
  {
    public static IObservable<Word.Document> DocumentBeforeSaveAsObservable(this Word.Application application)
    {
      return Observable.Create<Word.Document>(observer =>
      {
        Word.ApplicationEvents4_DocumentBeforeSaveEventHandler handler = observer.OnNext;
        application.DocumentBeforeSave += handler;

        return () => application.DocumentBeforeSave -= handler;
      });
    }
  }
公共静态类ApplicationExtensions
{
公共静态IObservable文档在保存为可观察之前(此词为.Application)
{
返回可观察的。创建(观察者=>
{
Word.ApplicationEvents4\u DocumentBeforeSaveEventHandler=observer.OnNext;
application.DocumentBeforeSave+=处理程序;
return()=>application.DocumentBeforeSave-=handler;
});
}
}
但是我收到错误“OnNext”没有重载匹配委托“Microsoft.Office.Interop.Word.ApplicationEvents4\u DocumentBeforeSaveEventHandler”

谁能给我指一下正确的方向吗

问候


Mike

您的问题是代表签名的问题

定义为
void(T值)

鉴于定义为
void(文档文档,参考bool savasui,参考bool Cancel)

如果您只需要发出
文档
(而不需要发出其他详细信息,如使其可取消),则可以执行以下操作:

public static IObservable<Word.Document> DocumentBeforeSaveAsObservable(
    this Word.Application application)
{
    return Observable.Create<Word.Document>(observer =>
    {
        Word.ApplicationEvents4_DocumentBeforeSaveEventHandler handler = 
            (doc, ref saveAsUI, ref cancel) => observer.OnNext(doc);

        application.DocumentBeforeSave += handler;

        return () => application.DocumentBeforeSave -= handler;
    });
}
然后你可以这样使用它:

public static IObservable<Word.Document> DocumentBeforeSaveAsObservable(
    this Word.Application application)
{
    return Observable.Create<Word.Document>(observer =>
    {
        Word.ApplicationEvents4_DocumentBeforeSaveEventHandler handler = 
            (doc, ref saveAsUI, ref cancel) => 
            {
                var args = new DocumentBeforeSaveEventArgs(doc, saveAsUI);

                observer.OnNext(args);

                cancel = args.Cancel;
            };

        application.DocumentBeforeSave += handler;

        return () => application.DocumentBeforeSave -= handler;
    });
}
public static IObservable documents在保存为可观察之前(
这个词。应用程序)
{
返回可观察的。创建(观察者=>
{
Word.ApplicationEvents4\u DocumentBeforeSaveEventHandler=
(doc,ref saveAsUI,ref cancel)=>
{
var args=保存事件args(doc,saveAsUI)之前的新文档;
OnNext观察员(args);
cancel=args.cancel;
};
application.DocumentBeforeSave+=处理程序;
return()=>application.DocumentBeforeSave-=handler;
});
}
public static IObservable<Word.Document> DocumentBeforeSaveAsObservable(
    this Word.Application application)
{
    return Observable.Create<Word.Document>(observer =>
    {
        Word.ApplicationEvents4_DocumentBeforeSaveEventHandler handler = 
            (doc, ref saveAsUI, ref cancel) => 
            {
                var args = new DocumentBeforeSaveEventArgs(doc, saveAsUI);

                observer.OnNext(args);

                cancel = args.Cancel;
            };

        application.DocumentBeforeSave += handler;

        return () => application.DocumentBeforeSave -= handler;
    });
}