• 自己查找 已选的
  • 询问社区
  • 实时帮助

请选择您需要帮助的产品

  • WindowsWindows
  • Internet ExplorerInternet Explorer
  • OfficeOffice
  • SurfaceSurface
  • Media PlayerMedia Player
  • SkypeSkype
  • Windows PhoneWindows Phone
  • 更多产品更多产品

如何: 使用自动运行,使用 C#.NET 创建 Office 命令栏和控件

文章编号: 303018 - 查看本文应用于的产品
展开全部 | 关闭全部

概要

本文演示如何实现自动化 Excel 创建包含按钮、 下拉列表框、 组合框和弹出式菜单的命令栏。
回到顶端 | 提供反馈

更多信息

Visual C#.NET 应用程序捕获,并响应各种命令栏控件将激发的单击并更改事件。虽然此示例为宿主应用程序使用 Excel,但命令栏代码将每个 Office 应用程序中起作用。

创建 C#.NET 自动化客户端

  1. 启动 Microsoft Visual Studio.NET。文件菜单上单击新建,然后单击项目项目类型下单击Visual C# 项目,然后在模板下单击Windows 应用程序默认情况下,将创建 Form1。
  2. 添加到Microsoft Excel 对象库Microsoft Office 对象库的引用。若要执行此操作,请执行以下步骤:
    1. 项目菜单上,单击添加引用
    2. COM选项卡上查找Microsoft Excel 对象库并单击选择

      注意: Microsoft Office 2003年包括主互操作程序集 (Pia)。Microsoft Office XP 中没有包含 Pia,但它们可能会被下载。有关 Office XP Pia 的其他信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:
      328912Microsoft Office XP Pia 将可供下载的信息:
    3. 单击确定中的添加引用对话框中,接受您的选择。
  3. 视图菜单上,单击工具箱以显示工具箱,然后将一个按钮添加到 Form1。
  4. 双击Button1为 Button1, onClick事件在打开代码窗口。到 Form1.cs 的顶部添加以下项:
    using Office = Microsoft.Office.Core;using Excel = Microsoft.Office.Interop.Excel;
  5. 在代码窗口中,将下面的代码
    private void button1_Click(object sender, System.EventArgs e){}
    使用:
    // Declare variables.Office.CommandBarButton oButton;Office.CommandBarComboBox oEdit;Office.CommandBarComboBox oDrop;Office.CommandBarComboBox oCombo; Office.CommandBarButton oPopupButton;private void button1_Click(object sender, System.EventArgs e){// Declare variables.Excel.Application oExcel;Office.CommandBar oCommandBar;Office.CommandBarPopup oPopup;Object oMissing = System.Reflection.Missing.Value;// Start Excel.oExcel = new Excel.Application();// Show Excel and set UserControloExcel.Visible = true;oExcel.UserControl = true;// Add a new workbookoExcel.Workbooks.Add(oMissing);// Create a new command bar.oCommandBar = oExcel.CommandBars.Add("Billiards Sample",oMissing,oMissing,// Add a button to the command bar.oButton = (Office.CommandBarButton)oCommandBar.Controls.Add(Office.MsoControlType.msoControlButton,oMissing,oMissing,oMissing,oMissing);// Set the caption and face ID.oButton.Caption = "New game";oButton.FaceId = 1845;// Set up a delegate for the Click event.Office._CommandBarButtonEvents_ClickEventHandler oButtonHandler = new Office._CommandBarButtonEvents_ClickEventHandler(oButton_Click);oButton.Click += oButtonHandler;// Add an edit box to the command bar.oEdit = (Office.CommandBarComboBox) oCommandBar.Controls.Add(Office.MsoControlType.msoControlEdit,oMissing,oMissing,oMissing,oMissing);// Show a vertical separator.oEdit.BeginGroup = true;// Clear the text and show a caption.oEdit.Text = "";oEdit.Caption = "Enter your name:";oEdit.Style = Office.MsoComboStyle.msoComboLabel;// Set up a delegate for the Change event.Office._CommandBarComboBoxEvents_ChangeEventHandler oEditHandler = new Office._CommandBarComboBoxEvents_ChangeEventHandler(oEdit_Change);oEdit.Change += oEditHandler;// Add a combo box to the command bar.oCombo = (Office.CommandBarComboBox) oCommandBar.Controls.Add(Office.MsoControlType.msoControlComboBox,oMissing,oMissing,oMissing,oMissing);// Add items to the combo box.oCombo.AddItem("Sharky",oMissing);oCombo.AddItem("Cash",oMissing);oCombo.AddItem("Lucky",oMissing);// Set the caption and style.oCombo.Caption = "Choose your opponent:";oCombo.Style = Office.MsoComboStyle.msoComboLabel;// Set up a delegate for the Change event.Office._CommandBarComboBoxEvents_ChangeEventHandler oComboHandler = new Office._CommandBarComboBoxEvents_ChangeEventHandler(oCombo_Change);oCombo.Change += oComboHandler;// Add a drop-down list box to the command bar.oDrop = (Office.CommandBarComboBox) oCommandBar.Controls.Add(Office.MsoControlType.msoControlDropdown,oMissing,oMissing,oMissing,oMissing);// Add items to the list box.oDrop.AddItem("8 Ball",oMissing);oDrop.AddItem("9 Ball",oMissing);oDrop.AddItem("Straight Pool",oMissing);oDrop.AddItem("Bowlliards",oMissing);oDrop.AddItem("Snooker",oMissing);// Set the value to the first in the list.oDrop.ListIndex = 1;// Set the caption and style.oDrop.Caption = "Choose your game:";oDrop.Style = Office.MsoComboStyle.msoComboLabel;// Set up a delegate for the Change event.Office._CommandBarComboBoxEvents_ChangeEventHandler oDropHandler = new Office._CommandBarComboBoxEvents_ChangeEventHandler(oDrop_Change);oDrop.Change += oDropHandler;// Add a pop-up menu to the command bar.oPopup = (Office.CommandBarPopup) oCommandBar.Controls.Add(Office.MsoControlType.msoControlPopup,oMissing,oMissing,oMissing,oMissing);// Add a separator before the pop-up button.oPopup.BeginGroup = true;// Set the caption.oPopup.Caption = "Rack 'em Up!";// Add a button to the pop-up.oPopupButton = (Office.CommandBarButton) oPopup.CommandBar.Controls.Add(Office.MsoControlType.msoControlButton,oMissing,oMissing,oMissing,oMissing);// Change the face ID and caption for the button.oPopupButton.FaceId = 643;oPopupButton.Caption = "Break!";// Set up a delegate for the Click event.Office._CommandBarButtonEvents_ClickEventHandler oPopupButtonHandler = new Office._CommandBarButtonEvents_ClickEventHandler(oPopupButton_Click);oPopupButton.Click += oPopupButtonHandler;// Show the command bar to the user.oCommandBar.Visible = true;}private void oButton_Click(Office.CommandBarButton Ctrl, ref bool Cancel){// Reset all values.oEdit.Text = "";oDrop.ListIndex = 1;oCombo.Text = "";Console.WriteLine("New game button clicked");}private void oEdit_Change(Office.CommandBarComboBox Ctrl){Console.WriteLine("oEdit_Change event fired -- Player's name = " + Ctrl.Text);}private void oCombo_Change(Office.CommandBarComboBox Ctrl){Console.WriteLine("oCombo_Change event fired -- New opponent = " + Ctrl.Text);}private void oDrop_Change(Office.CommandBarComboBox Ctrl){Console.WriteLine("oDrop_Change event fired -- Game type = " + Ctrl.Text);}private void oPopupButton_Click(Office.CommandBarButton Ctrl, ref bool Cancel){System.Random oRand;Console.WriteLine("oPopupButton_Click event fired");// Create a new random number class.oRand = new System.Random();String sWinner;// Get a random number and check its range.if (oRand.NextDouble() > 0.5) sWinner = oEdit.Text;elsesWinner = oCombo.Text;// Show a message box to the user.MessageBox.Show("Game: " + oDrop.Text + "\r\n\r\nName: " + oEdit.Text + "\r\nOpponent: " + oCombo.Text + "\r\n\r\nWinner: " + sWinner,"Game Results");} 
  6. 按 F5 键生成并运行该程序。
  7. 单击Button1以启动 Excel,插入一个新的命令栏,该命令栏上插入控件。

Office XP 的其他注意事项

Office XP 应用程序都有一个安全选项,以使 Visual Basic for Applications (VBA) 对象模型以编程方式访问。如果此设置为"off"(默认值),您可能会收到一个错误时运行该代码示例。有关此设置和如何纠正该错误的其他信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:
282830PRB: Office XP VBA 项目以编程方式访问被拒绝
回到顶端 | 提供反馈

参考

Office 自动化的详细信息,请参阅下面的 Microsoft Office 开发 Web 站点:
http://support.microsoft.com/ofd
回到顶端 | 提供反馈

属性

文章编号: 303018 - 最后修改: 2007年1月30日 - 修订: 5.6
这篇文章中的信息适用于:
  • Microsoft Visual C# .NET 2003 标准版
  • Microsoft Visual C# .NET 2002 标准版
  • Microsoft Office Excel 2003
  • Microsoft Excel 2002 标准版
关键字: 
kbmt kbautomation kbhowto KB303018 KbMtzh
机器翻译
注意:这篇文章是由无人工介入的微软自动的机器翻译软件翻译完成。微软很高兴能同时提供给您由人工翻译的和由机器翻译的文章, 以使您能使用您的语言访问所有的知识库文章。然而由机器翻译的文章并不总是完美的。它可能存在词汇,语法或文法的问题,就像是一个外国人在说中文时总是可能犯这样的错误。虽然我们经常升级机器翻译软件以提高翻译质量,但是我们不保证机器翻译的正确度,也不对由于内容的误译或者客户对它的错误使用所引起的任何直接的, 或间接的可能的问题负责。
点击这里察看该文章的英文版: 303018
Microsoft和/或其各供应商对于为任何目的而在本服务器上发布的文件及有关图形所含信息的适用性,不作任何声明。 所有该等文件及有关图形均"依样"提供,而不带任何性质的保证。Microsoft和/或其各供应商特此声明,对所有与该等信息有关的保证和条件不负任何责任,该等保证和条件包括关于适销性、符合特定用途、所有权和非侵权的所有默示保证和条件。在任何情况下,在由于使用或运行本服务器上的信息所引起的或与该等使用或运行有关的诉讼中,Microsoft和/或其各供应商就因丧失使用、数据或利润所导致的任何特别的、间接的、衍生性的损害或任何因使用而丧失所导致的之损害、数据或利润不负任何责任。
回到顶端 | 提供反馈

提供反馈

 
此信息是否有用?
有一点
对于自己而言,应用此篇文章要付出多大的努力?
非常低非常高
请告知原因并帮助我们了解应如何完善该信息
回到顶端回到顶端

文章翻译

(الشرق الاوسط (العربية
Office客户预览版