直接上代码了:

 

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 ;

最新文章

  1. 线程处理模型 由于 SynchronizationContext 引起的死锁问题解决
  2. 部署额外域控制器,Active Directory
  3. 一行命令搞定node.js 版本升级
  4. HDU 1102 最小生成树裸题,kruskal,prim
  5. BZOJ 1452 [JSOI2009] Count
  6. 使用SSH密钥连接Github
  7. Matlab程序怎样打包
  8. IOS init initWith 等相关集中
  9. ASP.NET Core教程【二】从保存数据看特有属性与服务端验证
  10. zookeeper 内部机制学习
  11. 【物联网】 9个顶级开发IoT项目的开源物联网平台(转)
  12. dp入门(LIS,LCS)
  13. Unity新版本VR以及SteamVR基础
  14. leetcode 数据库题解
  15. SpringMVC的 @RestController和@Controller 区别
  16. JAVA基础学习之路(十二)链表
  17. 一款基于HTML5 Canvas的画板涂鸦动画
  18. 在liberty中通过LTPA设置单点登录
  19. Win8运行金山词霸2005的问题
  20. 洛谷P4301 [CQOI2013]新Nim游戏(线性基)

热门文章

  1. PHP mysql 事务处理实例
  2. mysql 截取字符
  3. 第三节:卸载AppDomain
  4. python 关于 ImportError: No module named 的问题
  5. ruby on rails 实战(一)
  6. PagerAdapter的notifyDataSetChanged无效解决方法
  7. 算法系列9《MD5》
  8. .NET开源工作流RoadFlow-系统布署中常见错误及处理方法
  9. Oracle连乘聚合函数 MUL
  10. Oracle用户,权限,角色以及登录管理 scoot 授权