![jpg.gif](http://www.pdriver.com/bbs5/images/files/jpg.gif)
![jpg.gif](http://www.pdriver.com/bbs5/images/files/jpg.gif)
![jpg.gif](http://www.pdriver.com/bbs5/images/files/jpg.gif)
![jpg.gif](http://www.pdriver.com/bbs5/images/files/jpg.gif)
![jpg.gif](http://www.pdriver.com/bbs5/images/files/jpg.gif)
#region Read me for Add-in installation and setup information.
// When run, the Add-in wizard prepared the registry for the Add-in. // At a later time, if the Add-in becomes unavailable for reasons such as: // 1) You moved this project to a computer other than which is was originally created on. // 2) You chose 'Yes' when presented with a message asking if you wish to remove the Add-in. // 3) Registry corruption. // you will need to re-register the Add-in by building the MyAddin21Setup project // by right clicking the project in the Solution Explorer, then choosing install. #endregion /// <summary> /// The object for implementing an Add-in. /// </summary> /// <seealso class='IDTExtensibility2' /> [GuidAttribute("0E0C25E6-C16B-44F6-BEAA-192F86765DE8"), ProgId("EmailSum.Connect")] public class Connect : Object, Extensibility.IDTExtensibility2 { private Microsoft.Office.Interop.Outlook.Application applicationObject; private object addInInstance; private CommandBarButton btnGetEMailStats;/// <summary>
/// Implements the constructor for the Add-in object. /// Place your initialization code within this method. /// </summary> public Connect() { }/// <summary>
/// Implements the OnConnection method of the IDTExtensibility2 interface. /// Receives notification that the Add-in is being loaded. /// </summary> /// <param term='application'> /// Root object of the host application. /// </param> /// <param term='connectMode'> /// Describes how the Add-in is being loaded. /// </param> /// <param term='addInInst'> /// Object representing this Add-in. /// </param> /// <seealso class='IDTExtensibility2' /> public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom) { applicationObject = (Microsoft.Office.Interop.Outlook.Application) application; addInInstance = addInInst; }/// <summary>
/// Implements the OnDisconnection method of the IDTExtensibility2 interface. /// Receives notification that the Add-in is being unloaded. /// </summary> /// <param term='disconnectMode'> /// Describes how the Add-in is being unloaded. /// </param> /// <param term='custom'> /// Array of parameters that are host application specific. /// </param> /// <seealso class='IDTExtensibility2' /> public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode, ref System.Array custom) { if(disconnectMode != ext_DisconnectMode.ext_dm_HostShutdown) { OnBeginShutdown(ref custom); } applicationObject = null;}
/// <summary>
/// Implements the OnAddInsUpdate method of the IDTExtensibility2 interface. /// Receives notification that the collection of Add-ins has changed. /// </summary> /// <param term='custom'> /// Array of parameters that are host application specific. /// </param> /// <seealso class='IDTExtensibility2' /> public void OnAddInsUpdate(ref System.Array custom) { }/// <summary>
/// Implements the OnStartupComplete method of the IDTExtensibility2 interface. /// Receives notification that the host application has completed loading. /// </summary> /// <param term='custom'> /// Array of parameters that are host application specific. /// </param> /// <seealso class='IDTExtensibility2' /> public void OnStartupComplete(ref System.Array custom) { CommandBars commandBars = applicationObject.ActiveExplorer().CommandBars; try { // If our button is already // on the Standard CommandBar, use it. btnGetEMailStats = (CommandBarButton) commandBars["Standard"].Controls["Email统计"]; } catch { // OOPS! Our button is not there, so // we need to make a new instance. // Note that the Add() method was // defined to take optional parameters, // which are not supported in C#. // Thus we must specify Missing.value. btnGetEMailStats = (CommandBarButton) commandBars["Standard"].Controls.Add(1, System.Reflection.Missing.value, System.Reflection.Missing.value, System.Reflection.Missing.value, System.Reflection.Missing.value); btnGetEMailStats.Caption = "Email统计"; btnGetEMailStats.Style = MsoButtonStyle.msoButtonCaption; } // Setting the Tag property is not required, but can be used // to quickly reterive your button. btnGetEMailStats.Tag = "Email统计"; // Setting OnAction is also optional, however if you specify // the ProgID of the Add-in, the host will automatically // load the Add-in if the user clicks on the CommandBarButton when // the Add-in is not loaded. After this point, the Click // event handler is called. btnGetEMailStats.OnAction = "!<EMailStatsAddIn.Connect>"; btnGetEMailStats.Visible = true; // Rig-up the Click event for the new CommandBarButton type. btnGetEMailStats.Click += new _CommandBarButtonEvents_ClickEventHandler( btnGetEMailStats_Click); }private void btnGetEMailStats_Click(CommandBarButton Ctrl,
ref bool CancelDefault) { string statInfo; DateTime today = DateTime.Today; // The stats we are tracing. int eMailsToday = 0; int eMailsThisMonth = 0; int eMailSentToday = 0; int eMailSentThisMonth = 0; // Get items in user's inbox. NameSpace outlookNS = applicationObject.GetNamespace("MAPI"); MAPIFolder inboxFolder = outlookNS.GetDefaultFolder(OlDefaultFolders.olFolderInbox); // Compare time received to current day / month // and update our counters. foreach(object item in inboxFolder.Items) { MailItem mi = item as MailItem; if(mi != null) { if(mi.ReceivedTime.Day == today.Day) eMailsToday++; if(mi.ReceivedTime.Month == today.Month) eMailsThisMonth++; } } // Build first part of statInfo string. statInfo = string.Format("今天收到Email: {0}\n", eMailsToday); statInfo += string.Format("这个月收到Email: {0}\n", eMailsThisMonth); statInfo += "--------------------------------------\n"; // Get items in user's sent item folder and // test again. MAPIFolder SentFolder = outlookNS.GetDefaultFolder(OlDefaultFolders.olFolderSentMail); foreach(object item in SentFolder.Items) { // See if current item is a MailItem MailItem mi = item as MailItem; if(mi != null) { // It is, so get day/month stats. if(mi.SentOn.Day == today.Day) eMailSentToday++; if(mi.SentOn.Month == today.Month) eMailSentThisMonth++; } } // Build last part of statInfo string. statInfo += string.Format("今天发送Email: {0}\n", eMailSentToday); statInfo += string.Format("这个月发送Email: {0}\n", eMailSentThisMonth); // Show results. System.Windows.Forms.MessageBox.Show(statInfo, "Email统计结果:");}
/// <summary> /// Implements the OnBeginShutdown method of the IDTExtensibility2 interface. /// Receives notification that the host application is being unloaded. /// </summary> /// <param term='custom'> /// Array of parameters that are host application specific. /// </param> /// <seealso class='IDTExtensibility2' /> public void OnBeginShutdown(ref System.Array custom) { CommandBars commandBars = applicationObject.ActiveExplorer().CommandBars; try { // Find our button and kill it. commandBars["Standard"].Controls["GetEMailStats"].Delete( System.Reflection.Missing.value); } catch(System.Exception ex) {System.Windows.Forms.MessageBox.Show(ex.Message);}}
// private object applicationObject;// private object addInInstance; }}4. 我们运行安装程序,然后打开outlook我们可以看到:![jpg.gif](http://www.pdriver.com/bbs5/images/files/jpg.gif)
![jpg.gif](http://www.pdriver.com/bbs5/images/files/jpg.gif)
总结:
我们还可以在word、access等office软件实现相似的功能。