今天没事,看到一个用C#开发OutLook插件的例子,顺便自己做了一个

用C#开发OutLook插件
引言:
我们可以用VS.Net 2003开发OutLook插件,把自己的代码集成到OutLook中去。比如说我们可以在简单做一个邮件的统计功能。
软件原理:
利用C#调用Office的接口
代码实现:
1. 我们新建一个addin项目

jpg.gif此主题相关图片如下:


jpg.gif此主题相关图片如下:
按此在新窗口浏览图片

jpg.gif此主题相关图片如下:


jpg.gif此主题相关图片如下:
按此在新窗口浏览图片
这样我们根据.net的向导就自动生成了插件项目。
2. 如果你的应用程序中没有自动引用Microsoft.Office.Interop.Outlook.dll,那么我们手工来添加Microsoft.Office.Interop.Outlook.dll到引用中
3. 最终的界面:

jpg.gif此主题相关图片如下:
按此在新窗口浏览图片
Connect.cs最后的代码:
namespace EmailSum
{
 using System;
 using Microsoft.Office.Core;
 using Extensibility;
 using System.Runtime.InteropServices;
 using Microsoft.Office.Interop.Outlook;
 

 #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此主题相关图片如下:

点击[email统计]按钮,可以看到结果:

jpg.gif此主题相关图片如下:
按此在新窗口浏览图片

总结:
我们还可以在word、access等office软件实现相似的功能。

http://www.pdriver.com/bbs5/dispbbs.asp?BoardID=12&replyID=25531&id=134409&star=1&skin=0

转载于:https://www.cnblogs.com/martinxj/archive/2004/06/14/15547.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值