C# 如何在c中的word中添加菜单项#

C# 如何在c中的word中添加菜单项#,c#,C#,我已经尝试了右键单击word文档中添加新菜单项的代码。但有一个问题 错误:“this.Application”此行给出的语句错误不在当前上下文中。这条生产线或任何其他解决方案需要什么样的解决方案 Word.Application application; private void ThisAddIn_Startup(object sender, System.EventArgs e) { application = this.Application; application.W

我已经尝试了右键单击word文档中添加新菜单项的代码。但有一个问题

错误:“this.Application”此行给出的语句错误不在当前上下文中。这条生产线或任何其他解决方案需要什么样的解决方案

Word.Application application;

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    application = this.Application;
    application.WindowBeforeRightClick +=
        new Word.ApplicationEvents4_WindowBeforeRightClickEventHandler(application_WindowBeforeRightClick);

    application.CustomizationContext = application.ActiveDocument;

    Office.CommandBar commandBar = application.CommandBars["Text"];
    Office.CommandBarButton button = (Office.CommandBarButton)commandBar.Controls.Add(
        Office.MsoControlType.msoControlButton);
    button.accName = "My Custom Button";
    button.Caption = "My Custom Button";
}

public void application_WindowBeforeRightClick(Word.Selection selection, ref bool Cancel)
{
    if (selection != null && !String.IsNullOrEmpty(selection.Text))
    {
        string selectionText = selection.Text;

        if (selectionText.Contains("C#"))
            SetCommandVisibility("My Custom Button", false);
        else
            SetCommandVisibility("My Custom Button", true);
    }
}

private void SetCommandVisibility(string name, bool visible)
{
    application.CustomizationContext = application.ActiveDocument;
    Office.CommandBar commandBar = application.CommandBars["Text"];
    commandBar.Controls[name].Visible = visible;
}