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 ...
随机推荐
- Yii整合ZF2及soap实例
一)如何整合? // change the following paths if necessary $yii = dirname(__FILE__).'/framework/yii.php'; $c ...
- Java实现计算20的阶乘
循环从1乘到20,要注意的就是结果可能会很大,长度超出int类型的范围,所以定义乘积的时候用long. 代码如下: public class Practice3 { public static voi ...
- WIN10系统 Solidworks 2015 Toolbox插件提示 failed to create toolboxl ibrary object 解决方法
网上大部分都是说卸载一个更新程序,但是在WIN10中根本没有. 但是也可通过以下方法解决: 1.关闭SW程序及进程,用管理员命令打开CMD 2.打开并复制SW目录,默认为 C:\Program Fil ...
- 一个自定义的C#数据库操作基础类 SqlHelper
SqlHelper其实是我们自己编写的一个类,使用这个类目的就是让使用者更方便.更安全的对数据库的操作,既是除了在SqlHelper类以外的所有类将不用引用对数据库操作的任何类与语句,无须担心数据库的 ...
- Hadoop伪分布式搭建CentOS
所需软件及版本: jdk-7u80-linux-x64.tar.gz hadoop-2.6.0.tar.gz 1.安装JDK Hadoop 在需在JDK下运行,注意JDK最好使用Oracle的否则可能 ...
- 记一个python+sqlalchemy+tornado的一个高并发下,产生重复记录的bug
场景:在用户通过支付通道支付完成返回时,发现我收到的处理数据记录中有两条同样的数据记录, 也就是同一笔钱,我数据库中记为了两条一样的记录. tornado端代码 from tornado import ...
- python3.3中使用tornado.options.parse_config_file的时候,在windows下conf为utf-8时,报错的问题
由于我的windows7下的默认编码是gbk 在调用tornado.options.parse_config_file时,内部代码为 with open(path) as f: exec_in(f.r ...
- Unicode字符以16进制表示
int(x [,base ]) 将x转换为一个整数 long(x [,base ]) 将x转换为一个长整数 float(x ) 将x转换到一个 ...
- UVA 100 The 3*n+1 problem
UVA 100 The 3*n+1 problem. 解题思路:对给定的边界m,n(m<n&&0<m,n<1 000 000);求X(m-1<X<n+ ...
- (转)前端构建工具gulp入门教程
前端构建工具gulp入门教程 老婆婆 1.8k 2013年12月30日 发布 推荐 10 推荐 收藏 83 收藏,20k 浏览 本文假设你之前没有用过任何任务脚本(task runner)和命令行工具 ...