直接上代码了:

 

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. DevExpress控件使用经验总结- GridView列表行号显示操作

    DevExpress是一个比较有名的界面控件套件,提供了一系列的界面控件套件的DotNet界面控件.本文主要介绍我在使用DevExpress控件过程中,遇到或者发现的一些问题解决方案,或者也可以所示一 ...

  2. IT新人论成长

    说自己是新人,其实自己也不新了,2012年毕业,辗转3个城市,现在在上海一家公司,工资不到5K. 在来现在公司之前,我从事web后台开发,采用MVC模式,虽然做了不少的网站,但感觉自己的水平还是在底层 ...

  3. 自适应游标共享技术02(一个简单的例子来走近ACS)

    为了不让其他因素干扰实验,参数设置如下: optimizer_mode=ALL_ROWS(使用CBO) optimizer_features_enable=11.2.0.3(使用最新的优化参数) op ...

  4. Oracle 直方图实例测试

    --创建表 SQL> create table tab (a number, b number); Table created. --插入数据 SQL> begin .. loop ins ...

  5. 13.python中的字典

    字典其实和之前的元祖和列表功能相似,都是用来储存一系列对象的.也就是一种可变容器,或者是我所比喻的革新派的菜单. 但也不是完全相同,我在之前曾经将字典称为特殊的'序列',是字典拥有序列的部分特性,但是 ...

  6. 9 本免费的 Python 语言编程书籍(转载)

    9 本免费的 Python 语言编程书籍 原文地址:http://linuxtoy.org/archives/9-free-python-books.html 2010-03-03 Toy Poste ...

  7. [笔记]--Oracle 10g在Windows 32位系统使用2G以上内存

    1.修改c:\boot.ini文件 打开boot.ini文件,我的电脑->属性->高级->启动和恢复->编辑,设置在最后一行末尾添加/PAE选项后如下: [boot loade ...

  8. Oracle ODP.NET连接池

    数据库连接池 连接池是数据库连接的缓存,每当应用程序需要连接数据库时向连接池申请数据库连接,连接池负责具体数据库连接的创建和销毁.连接池中的数据库连接会缓存一段时间,后续的连接请求首先使用缓存中的数据 ...

  9. 请输入正确的RSA公钥

    没啥原因,换个浏览器就好使,原因是现有窗口登陆超时了.

  10. mysql开启日志

    在 centos 5 下,  在 mysld 下面,添加一行 log=/var/log/mysql.log 然后执行如下命令 touch /var/log/mysql.logchmod 777 /va ...