设为首页 收藏本站
查看: 2552|回复: 0

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

[复制链接]

尚未签到

发表于 2015-9-12 14:23:59 | 显示全部楼层 |阅读模式
用C#开发OutLook插件
引言:
我们可以用VS.Net 2003开发OutLook插件,把自己的代码集成到OutLook中去。比如说我们可以在简单做一个邮件的统计功能。
软件原理:
利用C#调用Office的接口
代码实现:
1. 我们新建一个addin项目

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

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

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

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

此主题相关图片如下:
DSC0005.jpg
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我们可以看到:

此主题相关图片如下:
DSC0006.jpg
点击[email统计]按钮,可以看到结果:

此主题相关图片如下:
DSC0007.jpg   
  总结:
我们还可以在word、access等office软件实现相似的功能。

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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-112747-1-1.html 上篇帖子: MS CRM 2011 为64位Outlook安装Silverlight 下篇帖子: outlook 当关闭时最小化到任务栏完美的解决方案
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表