C#Word加载项 - 功能区

时间:2012-08-20 09:59:44

标签: c# ms-office

我正在尝试创建一个简单的Word加载项。我已使用此自动生成的代码创建了Word 2010加载项项目:

public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject() {
            OwnRibbon ribbon = new OwnRibbon();
            //ribbon.DocumentProperty = //get the document here
            return ribbon;
        }
        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }

我查看了文档,并了解如何在此课程中向文档添加文本。然而,我有一个功能区(通过新项目创建 - &gt;功能区(视觉设计师)),带有两个按钮。

按下按钮时,我想在文档中添加文本。但是,此功能区创建了一个单独的类:

public partial class OwnRibbon
    {
        private void OwnRibbon_Load(object sender, RibbonUIEventArgs e)
        {

        }

        private void btnInvoegen_Click(object sender, RibbonControlEventArgs e)
        {
        }
    }

如何从点击事件处理程序访问文档?

由于

2 个答案:

答案 0 :(得分:3)

试试这段代码

Microsoft.Office.Tools.Word.Document vstoDocument =
 Globals.ThisAddIn.Application.ActiveDocument.GetVstoObject();

可以通过这样的静态方法访问很多Office对象。

答案 1 :(得分:1)

您可以在班级中创建一个属性,然后在ThisAddIn class:

中进行设置
protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject() {
            OwnRibbon ribbon = new OwnRibbon();
            ribbon.DocumentProperty = //get the document here
            return ribbon;
        }

OwnRibbon课程中:

private void btnInvoegen_Click(object sender, RibbonControlEventArgs e)
{
    //use DocumentProperty which holds the document
}