直接上代码了:

 

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. 神奇的fastcgi_finish_request

    当PHP运行在FastCGI模式时,PHP FPM提供了一个名为fastcgi_finish_request的方法.按照文档上的说法,此方法可以提高请求的处理速度,如果有些处理可以在页面生成完后再进行 ...

  2. C-链表的一些基本操作【创建-删除-打印-插入】

    #include <stdio.h> #include <stdlib.h> #include <malloc.h> #define LEN sizeof(stru ...

  3. 共享内存 share pool (2):BUCKET /FREE LISTS /RESERVED FREE LISTS /UNPINNED RECREATABLE CHUNKS (lru first)

    相关概念 BUCKET :每个bucket上挂有一个 chunk list.同一个BUCKET中的chunk在物理地址上是不一定相邻的 FREE LISTS:按bucket划分,共有255个,buck ...

  4. linux网络子系统内核分析

    1.选择路由 若要将数据包发至PC2,则linux系统通过查询路由表可知168.1.1.10(目的地址)的网关地址为192.168.1.1,此时linux系统选择网卡1发送数据包. 2.邻居子系统(通 ...

  5. virtualenv python虚拟环境搭建

    python virtualenv.py flask

  6. "Programming"和"Programming"是同一个"Programming"吗?

    什么意思? C语言没有专门的字符串类型,但是,它同样可以处理字符串.本文不是讨论字符串的使用,而是讨论C字符串之间的关系.如题,在C语言代码中,如果定义#define STR = "Prog ...

  7. windows 服务 安装 删除 启动 停止

    一.停止 sc stop 服务名 二.删除 sc delete 服务名 注意:有时删除不了,报什么“服务为删除标识” ,请将服务窗口关掉就好了. 三.创建 sc create XmlcSendServ ...

  8. Android开发遇到的异常及解决办法

    Android开发遇到的错误及解决方法1. Unable to resolve target 'android-7' 解决方案: 修改工程目录下的default.properties文件里的内容tar ...

  9. WdatePicker 动态变量表

    4. 日期范围限制静态限制 注意:日期格式必须与 realDateFmt 和 realTimeFmt 一致 你可以给通过配置minDate(最小日期),maxDate(最大日期)为静态日期值,来限定日 ...

  10. asp.net解决高并发的方案.

    asp.net解决高并发的方案. Posted on 2012-11-27 22:31 75077027 阅读(3964) 评论(1) 编辑 收藏 最近几天一直在读代震军的博客,他是 Discuz!N ...