向Excel文档中嵌入VBA控件和UserForm并显示

来源:未知 责任编辑:责任编辑 发表时间:2013-12-18 11:35 点击:

实现环境:Visual Studio 2010, Excel 2010, VSTO 4.0

在文档中嵌入一个Commandbutton点击它会显示一个UserForm,UserForm上有一个CommandButton点击会弹出消息框然后CommandButton的Caption改变。当UserForm关闭是文档中的CommandButton的Caption改变。

VB.NET:

[vb]
Imports Microsoft.Office.Tools.Ribbon 
Imports VBE = Microsoft.Vbe.Interop 
Imports Forms = Microsoft.Vbe.Interop.Forms 
 
Public Class Ribbon1 
    Private WithEvents objCommandButton As Forms.CommandButton 
 
    Private Sub Ribbon1_Load(ByVal sender As System.Object, ByVal e As  _ 
                             RibbonUIEventArgs) Handles MyBase.Load 
 
    End Sub 
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e _ 
As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button1.Click 
        Dim objApplicatin As Excel.Application = Globals.ThisAddIn.Application 
        Dim objWorkbook As Excel.Workbook = objApplicatin.ActiveWorkbook 
        Dim objWorksheet As Excel.Worksheet = objWorkbook.ActiveSheet 
        Dim objShape As Excel.Shape 
        Dim objOLEObject As Excel.OLEObject 
        Dim strModuleSnippet As String 
        Dim objVBAProject As VBE.VBProject 
        Dim objVBComponent As VBE.VBComponent 
        Dim objVBFormComponent As VBE.VBComponent 
        Dim objObjectFormButton As Object 
 
        objShape = objWorksheet.Shapes.AddOLEObject("Forms.CommandButton.1") 
        objShape.Name = "btn1" 
        objOLEObject = objWorksheet.OLEObjects("btn1") 
        Try 
            objCommandButton = TryCast(objOLEObject.Object, Forms.CommandButton) 
            objCommandButton.Caption = "Click Me" 
            strModuleSnippet = "private sub btn1_Click()" & Chr(13) & _ 
                "UserForm1.Show " & Chr(13) & "end sub" 

发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
用户名: 验证码:点击我更换图片
最新评论 更多>>

推荐热点

  • 浅析.NET下XML数据访问新机制
  • asp.net 面试+笔试题目第1/2页
  • C# 邮件地址是否合法的验证
  • C#高级编程:数据库连接[1]
  • asp.net 设置GridView的选中行的实现代码
  • 经典C++程序1
  • IIS 自动回收导致后台定时器失效的问题解决
  • ASP.NET GridView列表代码示例
  • 微软ASP.NET站点部署指南(3):使用Web.Config文件的Transforma
网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
Copyright © 2008-2015 计算机技术学习交流网. 版权所有

豫ICP备11007008号-1