我正在尝试创建一个简单的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
}我已经看过文档了,并且我知道如何从这个类向文档中添加文本。然而,我有一个功能区(创建通过新的项目->功能区(可视化设计器))与两个按钮。
当按钮被按下时,我想向文档中添加文本。但是,此功能区会创建一个单独的类:
public partial class OwnRibbon
{
private void OwnRibbon_Load(object sender, RibbonUIEventArgs e)
{
}
private void btnInvoegen_Click(object sender, RibbonControlEventArgs e)
{
}
}如何从单击事件处理程序访问文档?
谢谢
发布于 2012-08-20 18:08:17
您可以在类中创建属性,然后在ThisAddIn类中设置它:
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
}发布于 2012-08-20 18:22:42
试试这段代码
Microsoft.Office.Tools.Word.Document vstoDocument =
Globals.ThisAddIn.Application.ActiveDocument.GetVstoObject();许多Office对象都可以通过这样的静态方法进行访问。
https://stackoverflow.com/questions/12035652
复制相似问题