`
bolutes
  • 浏览: 866182 次
文章分类
社区版块
存档分类
最新评论

为Office文档添加Windows窗体控件:在运行时向应用程序级外接程序中添加控件

 
阅读更多

可以在运行时以编程方式向任何打开的文档中添加 Windows 窗体控件。首先,生成一个基于所打开的文档或工作表的宿主项。然后,使用新宿主项的 Document..::.Controls 属性的方法(在 Word 中),或者使用新宿主项的 Worksheet..::.Controls 属性的方法(在 Excel 中)。每个方法都有多个重载,使用这些重载,可以按不同的方式指定控件的位置。有关更多信息,请参见 Windows 窗体控件的帮助器方法

如果在运行时向某个文档中添加 Windows 窗体控件,那么,在关闭该文档后,该控件将不会保留在文档中。您可以在下次打开文档时重新创建该控件。有关更多信息,请参见在运行时向 Office 文档添加控件

有关在应用程序级项目中生成宿主项的更多信息,请参见在运行时在应用程序级外接程序中扩展 Word 文档和 Excel 工作簿

在运行时添加 Windows 窗体控件

  • 使用名称为 Add<控件类>(其中控件类 是要添加的 Windows 窗体控件的类名,如 AddButton)的 Microsoft.Office.Tools.Word..::.ControlCollection 方法(对于 Word 项目)或 Microsoft.Office.Tools.Excel..::.ControlCollection 方法(对于 Excel 项目)。

    下面的代码示例演示如何使用 Word 外接程序,向活动文档的第一段中添加 Button

    Visual Basic
    Dim salesButton As Microsoft.Office.Tools.Word.Controls.Button
    Dim extendedDocument As Document = Me.Application.ActiveDocument.GetVstoObject()
    
    extendedDocument.Paragraphs(1).Range.InsertParagraphBefore()
    salesButton = extendedDocument.Controls.AddButton( _
        extendedDocument.Paragraphs(1).Range, 75, 18, "salesButton")
    salesButton.Text = "Calculate Sales"
    
    C#
    Microsoft.Office.Tools.Word.Controls.Button salesButton;
    Document extendedDocument = this.Application.ActiveDocument.GetVstoObject();
    
    extendedDocument.Paragraphs[1].Range.InsertParagraphBefore();
    salesButton = extendedDocument.Controls.AddButton(
        extendedDocument.Paragraphs[1].Range, 75, 18, "salesButton");
    salesButton.Text = "Calculate Sales";
    
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics