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 ...
随机推荐
- Dalvik opcodes
原文地址: http://pallergabor.uw.hu/androidblog/dalvik_opcodes.html Dalvik opcodes Author: Gabor Paller V ...
- 【转】Messagedlg
) = mrYes then Close; MessageDlg用法 对话框类型:mtwarning——含有感叹号的警告对话框mterror——含有红色叉符号的错误对话框mtinformation ...
- 【Django】Apache上运行多个Django项目
运行单个项目的步骤参考:这里 1 安装环境 操作系统:Ubuntu 12.04 LTS 32 位(安装在VMware虚拟机中) python 版本: Python 2.7.3 Django版本 > ...
- java-脚本-编译-注解
有注解没注解生成字节码一样 ,只对处理它的工具有用通过注解接口定义@interface 元注解(4个)@Target ANNOTATION_TYPE/PACKAGE/TYPE/METHOD/CONST ...
- 【FAQ】【JSP】HTTP Status 500 - Summary(问题排查时候应该仔细分析所有的错误打印说明)
Question 1.HTTP Status 500 - Unable to compile class for JSP:'***' cannot be resolved to a type 原因分析 ...
- Excle快速输入√与×
如何在EXCLE中快速输入√与×呢 很简单的一个小技巧,只需要在EXCLE单元格中输入P(O) 然后将其字体设置为Wingdings 2,接着就出现符号了 不信你可以试试的哦.
- Tomcat 服务器服务的注册修改删除
1. 注册Tomcat服务 运行cmd,切换目录到tomcat/bin, 执行以下命令service.bat install 2.删除Tomcat服务
- Eclipse基金会
昨天Eclipse基金会庆祝其成立十周年.2004年2月的新闻稿宣布该非盈利组织的正式成立,由包括开发者.消费者和插件提供商在内的各独立团体组成的董事会,为Eclipse的长期发展负责. 基金会成立时 ...
- postgresql 连接数
改文件 postgresql.conf 里的 #max_connections=32 为 max_connections=1024 以及另外相应修改 share_buffer 参数. 执行SELECT ...
- Mysql 更改最大连接数
方法一: 进入MySQL安装目录 打开MySQL配置文件 my.ini 或 my.cnf查找 max_connections=100 修改为 max_connections=1000 服务里重起MyS ...