使用VS2008进行VSTO-Addin实战开发-创建自己的菜单(一)

操作系统:Windows Vista
开发环境:Visual Studio 2008 Beta2
运行环境:Microsoft office 2007(Outlook)

初次使用VSTO,代码主要示例了如何创建菜单,以及响应菜单的Click事件来创建一个MailItem。

  private   void  ThisAddIn_Startup( object  sender, System.EventArgs e)
        
{
            
this.AddMenu();
        }


        
private   void  ThisAddIn_Shutdown( object  sender, System.EventArgs e)
        
{
           
// MessageBox.Show("Stoped");
        }



        
private   void  AddMenu()
        
{
            
//获取Outlook的MenuBar
            Office.CommandBar bar = this.Application.ActiveExplorer().CommandBars.ActiveMenuBar;
            
//创建顶级菜单
            Office.CommandBarControl menuTop = bar.Controls.Add(Office.MsoControlType.msoControlPopup, Type.Missing, Type.Missing, bar.Controls.Count, true);
            menuTop.Caption 
= "我的菜单";

            Office.CommandBarPopup commandBarPopupTmp
= menuTop.Control as Office.CommandBarPopup;
           
            Office.CommandBarControl menuGroup 
= null;
            Office.CommandBarControl menuCreateMail 
= null;

            
if (commandBarPopupTmp.Controls.Count == 0)
            
{
                menuGroup 
= commandBarPopupTmp.Controls.Add(Office.MsoControlType.msoControlPopup, Type.Missing, Type.Missing, Type.Missing, true);
            }

            
else
            
{
                menuGroup 
= commandBarPopupTmp.Controls.Add(Office.MsoControlType.msoControlPopup, Type.Missing, Type.Missing, commandBarPopupTmp.Controls.Count, true);
            }


            menuCreateMail 
= commandBarPopupTmp.Controls.Add(Office.MsoControlType.msoControlButton, Type.Missing, Type.Missing, 1true);

            menuGroup.Caption 
= "菜单组";

            menuCreateMail.Caption 
= "创建我的邮件";

            Office.CommandBarButton buttonMenu 
= menuCreateMail as Office.CommandBarButton;
            buttonMenu.Click 
+= new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(buttonMenu_Click);
        }


        
void  buttonMenu_Click(Microsoft.Office.Core.CommandBarButton Ctrl,  ref   bool  CancelDefault)
        
{
            Outlook.MailItem mail 
= this.Application.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem) as Outlook.MailItem;
            mail.To 
= "[email protected]";
            mail.Subject 
= "Test mail";
            mail.Body 
= "Hi, this is a test.";
           
            mail.Display(Type.Missing);

            
//mail.Send();
        }

示例代码下载
/Files/jetxia/VSTO/VSTO_MyMenu.zip

你可能感兴趣的:(vs2008)