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 ;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值