0

我试过

var wordApp = new Microsoft.Office.Interop.Word.Application();
var doc = wordApp.Documents.Open(FileName);
wordApp.Visible = true;

   ((Microsoft.Office.Interop.Word.ApplicationEvents4_Event)wordApp.Quit) += new ApplicationEvents4_QuitEventHandler(delegate
                    {
                        MessageBox.Show("word closed!");
                    });

但我得到:

Cannot convert method group 'Quit' to non-delegate type 'Microsoft.Office.Interop.Word.ApplicationEvents4_Event'. Did you intend to invoke the method?


Microsoft.Office.Interop.Word._Application.Quit(ref object, ref object, ref object)' 
and non-method 'Microsoft.Office.Interop.Word.ApplicationEvents4_Event.Quit'. Using method group.

由于警告,我做了演员表,但没有解决。而且我不知道如何解决这个错误。提前致谢。

4

1 回答 1

1

您在强制转换表达式中放错了括号,您不想强制转换退出。正确的语法是:

((Microsoft.Office.Interop.Word.ApplicationEvents4_Event)wordApp).Quit += ...

也许您可以通过使用using指令更轻松地避免麻烦,这样您就不需要填塞表达式并且可以编写更具可读性的代码:

using Word = Microsoft.Office.Interop.Word;
...

    var wordApp = new Word.Application();
    var doc = wordApp.Documents.Open(FileName);
    wordApp.Visible = true;
    var events = (Word.ApplicationEvents4_Event)wordApp;
    events.Quit += delegate {
        MessageBox.Show("word closed!");
    };
于 2012-06-09T16:17:06.457 回答