You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# Word加载项开发报错:Application_DocumentOpen无匹配委托求解

Hey there! Let's break down this error and get your Word add-in's event handling working properly.

The Root Cause of Error CS0123

That error pops up because your Application_DocumentOpen method's signature doesn't match what the ApplicationEvents4_DocumentOpenEventHandler delegate expects. Word's interop library requires this event handler to accept two specific parameters: an object sender, and a Word.DocumentEventArgs argument (which lets you access the document that was just opened).

Fixing the Event Handler

Here's how to adjust your code to resolve the mismatch:

  1. First, make sure your event handler method has the correct signature:
private void Application_DocumentOpen(object sender, Word.DocumentEventArgs e)
{
    // Add your custom logic here
    Word.Document openedDocument = e.Document;
    MessageBox.Show($"Successfully opened document: {openedDocument.Name}");
}
  1. Keep your existing event registration code in ThisAddIn_Startup (it's already correct):
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    this.Application.DocumentOpen += new Word.ApplicationEvents4_DocumentOpenEventHandler(Application_DocumentOpen);
}

Full Working Example

For context, here's what your complete ThisAddIn class might look like:

using Microsoft.Office.Tools.Word;
using Word = Microsoft.Office.Interop.Word;
using System.Windows.Forms;

namespace YourWordAddInProjectName
{
    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            // Register the DocumentOpen event
            this.Application.DocumentOpen += new Word.ApplicationEvents4_DocumentOpenEventHandler(Application_DocumentOpen);
        }

        private void Application_DocumentOpen(object sender, Word.DocumentEventArgs e)
        {
            // Example logic: Show a message with the opened document's path
            var doc = e.Document;
            MessageBox.Show($"Document opened: {doc.FullName}");
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            // Optional: Unregister the event to clean up resources
            this.Application.DocumentOpen -= Application_DocumentOpen;
        }

        #region VSTO Generated Code
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        #endregion
    }
}

Bonus: Simplified Syntax

If you prefer, you can skip explicitly creating the delegate instance (modern C# will infer it automatically):

this.Application.DocumentOpen += Application_DocumentOpen;

Or even use a lambda expression to write the logic inline without a separate method:

this.Application.DocumentOpen += (sender, e) => 
{
    MessageBox.Show($"Opened document: {e.Document.Name}");
};

Just double-check that you have the correct Word interop assembly referenced in your project (the VSTO template usually adds this automatically, but it's worth verifying if you run into other issues).

内容的提问来源于stack exchange,提问作者Parth SHah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:57:20