从 Silverlight 4 开始,启动 Word 并让用户执行某些操作非常容易:
dynamic word = System.Runtime.InteropServices.Automation.CreateObject("Word.Application");
word.Visible = true;
word.Documents.Open("test.doc");
MS Word 公开退出事件[1]。我想处理这个事件,但我一辈子都不知道如何处理。我尝试这样做:
public delegate void WordQuitEventHandler(object sender, ref bool cancel);
public event WordQuitEventHandler OnQuit;
private void WordOnQuit(dynamic sender, ref bool cancel)
{
if (OnQuit != null)
{
OnQuit(this, ref cancel);
}
}
然后做
word.Quit = WordOnQuit;
或
word.Quit += WordOnQuit;
但是无法将 WordOnQuit 的委托(delegate)分配给动态对象 word.Quit。那么如何捕获这个事件呢?
[1] http://msdn.microsoft.com/en-us/library/aa211898(v=office.11).aspx
最佳答案
为了完整起见,您要找的人是......
AutomationEvent quitEvent = AutomationFactory.GetEvent(word,"Quit");
quitEvent.EventRaised += new EventHandler<AutomationEventArgs>(quitEvent_EventRaised);
当然,您可以根据需要内联回调。
此外,我发现此事件可能有点不稳定。大多数时候它会触发...大多数时候:-)
HTH。
关于c# - 使用 C# 中的动态 com 互操作处理来自 Word 的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3513208/