直接上代码了:

 

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 ;

VSTO Word2003 添加菜单栏, 添加工具栏的更多相关文章

  1. Ext.grid.EditorGridPanel点击单元格添加菜单栏

    1.定义菜单栏需要的全局变量 var khbm; var type; 2.新建一个菜单栏 var smenu = new Ext.menu.Menu({ id:"sMenu", i ...

  2. 【MFC - 菜单】在对话框程序中添加菜单栏(CMenu)(转)

    原文转自 http://lishiqiang1988.blog.163.com/blog/static/41147912201382104631547/ VS2010的MFC对话框程序中添加菜单栏的过 ...

  3. [ZETCODE]wxWidgets教程四:菜单栏和工具栏

    本教程原文链接:http://zetcode.com/gui/wxwidgets/menustoolbars/ 翻译:瓶哥 日期:2013年11月28日星期四 邮箱:414236069@qq.com ...

  4. Qt 学习之路 :菜单栏、工具栏和状态栏

    在之前的<添加动作>一文中,我们已经了解了,Qt 将用户与界面进行交互的元素抽象为一种“动作”,使用QAction类表示.QAction可以添加到菜单上.工具栏上.期间,我们还详细介绍了一 ...

  5. 三、PyQt5不同方法创建菜单栏、工具栏和状态栏

    创建菜单栏.工具栏和状态栏可以直接通过代码实现,也可以通过Qt Designer中的属性编辑器等实现.通过两种方法的学习可以加深理解,更好的掌握PyQt5. 一.菜单栏与状态栏 状态栏的设置比较简单, ...

  6. QT学习之菜单栏与工具栏

    QT学习之菜单栏与工具栏 目录 简单菜单栏 多级菜单栏 上下菜单栏 工具栏 简单菜单栏 程序示例 from PyQt5.QtWidgets import QApplication, QMainWind ...

  7. Python pyQt4/pyQt5 学习笔记2(状态栏、菜单栏和工具栏)

    例子:状态栏.菜单栏和工具栏 import sys from PyQt4 import QtGui class Example(QtGui.QMainWindow): def __init__(sel ...

  8. 【PyQt5 学习记录】005:QMainWindow 及状态栏、菜单栏和工具栏

    #!/usr/bin/env python import sys from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QA ...

  9. Qt——菜单栏、工具栏、状态栏

    1.菜单栏 菜单栏的意义是将可点击触发最终事件的集中在一起,所以菜单栏中是QAction 添加菜单栏是QMainWindow的行为 QMenubar *menubar = this->addMe ...

随机推荐

  1. PHP实现的一分页工具类代码

    总的页数是一个长度一定的木块,这把尺子在这个木块上滑动,前提,尺子的两端不能超出木块:D.发现这么一来要做的事情就是去找这个尺子在木块上的起始点,根据用户给传进来的page变量.哈哈,关键代码下面: ...

  2. C++十进制转换为二进制

    题目内容:将十进制整数转换成二进制数. 输入描述:输入数据中含有不多于50个的整数n(-231<n<231). 输出描述:对于每个n,以11位的宽度右对齐输入n值,然后输出“-->” ...

  3. oracle 几个时间函数探究

    近来经常用到时间函数,在此写一个笔记,记录自己的所得,希望也对您有所帮助. 1.对于一个时间如 sysdate:2015/1/30 14:16:03如何只得到年月日,同时它的数据类型不变化呢? 最容易 ...

  4. 6.24 AppCan移动开发者大会:议程重大更新,报名即将关闭

    大会倒计时2天,议程重大更新,报名通道即将关闭! 创业6年,由AppCan主办的第一届移动开发者大会将在本周五盛大召开.超过100万开发者线上参与.现场1500人规模.50家移动互联企业深度参与.30 ...

  5. Android--将字节数转化为B,KB,MB,GB的方法

    //将字节数转化为MB private String byteToMB(long size){ long kb = 1024; long mb = kb*1024; long gb = mb*1024 ...

  6. hdu 1973 Prime Path

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Description The ministers of the cabi ...

  7. Douglas Crockford: entityify & deentityify

    大神之字符与字符实体的相互转换方法 // & to & if (!String.prototype.entityify) { String.prototype.entityify = ...

  8. C#中的索引

    索引代码示例:                 索引的使用示例:                       索引在经过编译器编译之后就相当于一个属性,进一步的说就是两个方法.       一个类允许 ...

  9. C#和ASP.Net面试题目集锦

    1.有哪几种方法可以实现一个类存取另外一个类的成员函数及属性,并请举列来加以说明和分析.2.A类是B类的基类,并且都有自己的构造,析构函数,请举例证明B类从实例化到消亡过程中构造,析构函数的执行过程. ...

  10. IOS 其他 - 如何让 app 支持32位和64位

    让App支持32-bit和64-bit基本步骤 1.确保Xcode版本号>=5.0.1 2.更新project settings, minimum deployment target >= ...