在Excel中用代码嵌入TextBox并设定Chage事件。

本示例在Visual Studio 2010, Office 2010, VSTO 4.0的环境下完成。

在工程必须手工添加"Microsoft.Vbe.Interop.Forms"的引用。

本示例完成的关键在于Microsoft.Vbe.Interop.Forms.TextBox 变量必须是Class-level的(作用范围在整个类以内)。否则事件不会被触发。

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using Microsoft.Office.Tools.Excel;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using MF = Microsoft.Vbe.Interop.Forms;

namespace ExcelWorkbook7
{
    public partial class ThisWorkbook
    {
        private MF.TextBox textBox1 = null; //这一步很关键,否刚事件不会被触发
        //这个是与Outlook 2007中的CommandButton一样的。

        private void ThisWorkbook_Startup(object sender, System.EventArgs e)
        {
            Excel.Workbook objWorkbook = Application.ActiveWorkbook;
            Excel.Worksheet objWorksheet = objWorkbook.ActiveSheet;
            //添加一个Shape——其实就是添加一个TextBox
            /* 控件名称 	控件类 
             * 复选框 		Forms.CheckBox.1 
             * 组合框 		Forms.ComboBox.1 
             * 命令按钮 	Forms.CommandButton.1 
             * 框架 		Forms.Frame.1 
             * 图像 		Forms.Image.1 
             * 标签 		Forms.Label.1 
             * 列表框 		Forms.ListBox.1 
             * 多页控件 	Forms.MultiPage.1 
             * 选项按钮 	Forms.OptionButton.1 
             * 滚动条 		Forms.ScrollBar.1 
             * 数值调节钮 	Forms.SpinButton.1 
             * TabStrip 	Forms.TabStrip.1 
             * 文本框 		Forms.TextBox.1 
             * 切换按钮 	Forms.ToggleButton.1 
             * 其他控件的类名需要自行查找
             */
            var objTextBox = objWorksheet.Shapes.AddOLEObject("Forms.TextBox.1",
Type.Missing, false, false, Type.Missing, Type.Missing, Type.Missing, 2, 2, 100,
20);
            objTextBox.Name = "TextBox1";   //这行必须的。它直接影响下一行。
            Excel._OLEObject objOLEObject = objWorksheet.OLEObjects("TextBox1");
            if (objOLEObject.Object is MF.TextBox)
            {
                textBox1 = (MF.TextBox)objOLEObject.Object;
                textBox1.Change +=
                    new MF.MdcTextEvents_ChangeEventHandler(textBox1_Change);
            }

        }

        void textBox1_Change()
        {
            MessageBox.Show(textBox1.Text);
        }

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

        #region VSTO Designer 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(ThisWorkbook_Startup);
            this.Shutdown += new System.EventHandler(ThisWorkbook_Shutdown);
        }

        #endregion

    }
}

相关资源:http://download.csdn.net/detail/tx_officedev/3928495

posted @ 2011-12-16 12:55  许阳 无锡  阅读(1195)  评论(0编辑  收藏  举报