zoukankan      html  css  js  c++  java
  • VSTO Word2003 添加菜单栏, 添加工具栏

    直接上代码了:

    Microsoft.Office.Core.CommandBar menuBar;
    CommandBarButton ccbtn = null;     

    CommandBarButton btnRequirementProperty;

    CommandBarButton btnCancelImport;

    CommandBarButton btnCancelImport ;

    Office.CommandBarButton btn ;


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

                //this.AddMenu();
                //return;

                Word.Application wordApp = this.Application;
                wordApp.Visible = true;
                //Microsoft.Office.Interop.Word.Application Application.
                // this.Application = this.GetHostItem<Microsoft.Office.Interop.Word.Application>(typeof(Microsoft.Office.Interop.Word.Application), "Application");
                //this.Application.ActiveDocument
                //Create a Command Bar
                object missing = System.Reflection.Missing.Value;
               
               
                menuBar = ((Microsoft.Office.Interop.Word.Application)wordApp).CommandBars.ActiveMenuBar;// ["Menu Bar"];
                //CommandBar toolBar = wordApp.CommandBars["Menu Bar"];           

                //删除菜单上次生成的菜单.
                Microsoft.Office.Core.CommandBarControls bars = menuBar.Controls;
                foreach (Microsoft.Office.Core.CommandBarControl temp_contrl in bars)
                {
                    string t = temp_contrl.Tag;
                    //如果已经存在就删除
                    if (t == "导入")
                    {
                        temp_contrl.Delete(false);
                    }
                }
                menuBar.Visible = true;


                //添加菜单栏 菜单
                // 一级菜单
                //
                Microsoft.Office.Core.CommandBarPopup popuBar =
                    (Microsoft.Office.Core.CommandBarPopup)menuBar.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlPopup,
                missing, missing, missing, true);
                popuBar.Tag = "导入";
                popuBar.Caption = "导入";
                popuBar.Visible = true;


                // 二级菜单
                 btnRequirementProperty =
                    (CommandBarButton)popuBar.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton, 1, "", 1, true);
                btnRequirementProperty.Caption = "单条导入";
                btnRequirementProperty.Visible = true;
                btnRequirementProperty.Style = MsoButtonStyle.msoButtonIconAndCaption;
                btnRequirementProperty.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.btn_Click);

                btnCancelImport =
                    (CommandBarButton)popuBar.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton, 1, "", 1, true);
                btnCancelImport.Caption = "批量导入";
                btnCancelImport.Visible = true;
                btnCancelImport.Style = MsoButtonStyle.msoButtonCaption;
                btnCancelImport.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.btn_Click);          

                //添加工具栏  工具
                Office.CommandBar commandBar = wordApp.CommandBars.Add("My Bar", Office.MsoBarPosition.msoBarTop, false, true);
                commandBar.Visible = true;

                ////Add a Command Bar button
                btn = commandBar.Controls.Add(Office.MsoControlType.msoControlButton,
                    missing, missing, missing, true) as Office.CommandBarButton;
                btn.Caption = "My 233332";
                btn.Tag = "MyButton";
                btn.Style = MsoButtonStyle.msoButtonCaption;
                btn.Click += new _CommandBarButtonEvents_ClickEventHandler(btn_Click);
            }

            void btn_Click(CommandBarButton Ctrl, ref bool CancelDefault)
            {
                MessageBox.Show("My button is clicked!" + DateTime.Now);

            }

    出现的问题及解决方案:

    1. 每次执行菜单都会重新添加菜单. 我这里的解决先删除之前的菜单, 重新添加菜单

    Microsoft.Office.Core.CommandBarControls bars = menuBar.Controls;
                foreach (Microsoft.Office.Core.CommandBarControl temp_contrl in bars)
                {
                    string t = temp_contrl.Tag;
                    //如果已经存在就删除
                    if (t == "导入")
                    {
                        temp_contrl.Delete(false);
                    }
                }

    2. 添加菜单及工具栏的事件只能执行一次.   第二次就没反应了.  将按钮的变量设置成类的成员变量.   不能放在方法内

    CommandBarButton btnRequirementProperty;

    CommandBarButton btnCancelImport;

    Office.CommandBarButton btn ;

  • 相关阅读:
    MySQL客户端执行外部sql文件命令
    Java nextInt()函数
    JSP
    托管和非托管的区别。
    FTP软件Filezilla出现“读取目录列表失败”的解决办法
    Fiddler 抓包工具总结
    wampserver:Could not execute menu item.
    重装系统怎么恢复wampserver数据
    同时安装Xcode6和Xcode7导致出现N多UUID 模拟器解决办法
    打印沙漏
  • 原文地址:https://www.cnblogs.com/chencidi/p/3975352.html
Copyright © 2011-2022 走看看