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 ;
VSTO Word2003 添加菜单栏, 添加工具栏的更多相关文章
- Ext.grid.EditorGridPanel点击单元格添加菜单栏
1.定义菜单栏需要的全局变量 var khbm; var type; 2.新建一个菜单栏 var smenu = new Ext.menu.Menu({ id:"sMenu", i ...
- 【MFC - 菜单】在对话框程序中添加菜单栏(CMenu)(转)
原文转自 http://lishiqiang1988.blog.163.com/blog/static/41147912201382104631547/ VS2010的MFC对话框程序中添加菜单栏的过 ...
- [ZETCODE]wxWidgets教程四:菜单栏和工具栏
本教程原文链接:http://zetcode.com/gui/wxwidgets/menustoolbars/ 翻译:瓶哥 日期:2013年11月28日星期四 邮箱:414236069@qq.com ...
- Qt 学习之路 :菜单栏、工具栏和状态栏
在之前的<添加动作>一文中,我们已经了解了,Qt 将用户与界面进行交互的元素抽象为一种“动作”,使用QAction类表示.QAction可以添加到菜单上.工具栏上.期间,我们还详细介绍了一 ...
- 三、PyQt5不同方法创建菜单栏、工具栏和状态栏
创建菜单栏.工具栏和状态栏可以直接通过代码实现,也可以通过Qt Designer中的属性编辑器等实现.通过两种方法的学习可以加深理解,更好的掌握PyQt5. 一.菜单栏与状态栏 状态栏的设置比较简单, ...
- QT学习之菜单栏与工具栏
QT学习之菜单栏与工具栏 目录 简单菜单栏 多级菜单栏 上下菜单栏 工具栏 简单菜单栏 程序示例 from PyQt5.QtWidgets import QApplication, QMainWind ...
- Python pyQt4/pyQt5 学习笔记2(状态栏、菜单栏和工具栏)
例子:状态栏.菜单栏和工具栏 import sys from PyQt4 import QtGui class Example(QtGui.QMainWindow): def __init__(sel ...
- 【PyQt5 学习记录】005:QMainWindow 及状态栏、菜单栏和工具栏
#!/usr/bin/env python import sys from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QA ...
- Qt——菜单栏、工具栏、状态栏
1.菜单栏 菜单栏的意义是将可点击触发最终事件的集中在一起,所以菜单栏中是QAction 添加菜单栏是QMainWindow的行为 QMenubar *menubar = this->addMe ...
随机推荐
- JSON (仅限本地)
<script type="text/javascript"> setInterval(function() { $("#content").loa ...
- Android———从GitHub上下载源码的方法【Written By KillerLegend】
首先声明,本文说的是从GitHub上下载源码而非上传源码! 1:下载tortoisegit,下载地址为: https://code.google.com/p/tortoisegit/wiki/Down ...
- python学习应用笔记(一)
之前一直用c++写程序 所以考虑程序一般都比较容易往数据结构的方向想 而自己设计数据结构往往要费很大事 昨天看了一下python 发现脚本语言 真是厉害 用来进行模拟运算确实不错 可以先 ...
- 排序 选择排序&&堆排序
选择排序&&堆排序 1.选择排序: 介绍:选择排序(Selection sort)是一种简单直观的排序算法.它的工作原理如下.首先在未排序序列中找到最小(大)元素,存放到排序序列的起始 ...
- 11G ORACLE RAC DBCA 无法识别asm磁盘组
ASM磁盘无法识别几种现象: 1) gi家目录或者其子目录权限错误 2)asm磁盘的权限错误 3)asm实例未启动或者asm磁盘组没有mount上 4)asm磁盘组资源没有在线 5)oracle用户的 ...
- Mongodb shell 基本操作
/opt/mongodb-2.6.6/bin > mongo 1. 查询本地所有数据库名称> show dbs 2. 切换至指定数据库环境(若无指定的数据库,则创建新的库)> use ...
- OpenGL 纹理贴图
前一节实例代码中有个贴图操作. 今天就简单说明一下纹理贴图... 为了使用纹理贴图.我们首先需要启用纹理贴图功能. 我们可以在Renderer实现的onSurfaceCreated中定义启用: // ...
- android Init 相关分析
Init.c主要工作 1. 初始化属性(包括建立/dev./proc等目录.初始化属性.log.执行init.rc等初始化文件中的action等). 2. 解析配置文件的命令(主要是init.rc文件 ...
- JavaScript高级程序设计之基本包装类型
为便于操作基本类型值,ECMAScript提供了3个特殊的引用类型:Boolean, Number 和 String // 字符串怎么会有方法呢 var str1 = "some text& ...
- 无法在 Android 模拟器上访问本机的Web服务的解决办法
我在本地跑了一个 Tomcat ,我想在 Android 模拟器中直接通过下面的 url 地址访问 Tomcat 上的服务 http://192.168.0.20:8080/getweather 但是 ...