c# outlook

c# outlook
Outlook & C#
 
 
How to create Outlook 2003 items in C# ?


For accesing Office 2003 you'll need the Office XP primary interop assemblies (you can download them here). Once you got them you can start programming Office programs with C# :).

 

So, start up MS Visual C#.NET and create new project (WinForms or Console) then add a reference to Microsoft.Office.Interop.Outlook.

 

First thing you need to do is create an Outlook Application object. With this object you can programmatically control Outlook.

 

using Outlook = Microsoft.Office.Interop.Outlook;

// Create an Outlook Application object

Outlook.Application outlookApp = new Outlook.Application ();

 

Once you have an Application type you can create Outlook items. Items are represented by the Microsoft.Office.Interop.Outlook.OlItemType enumeration (olAppointmentItem, olContactItem, olTaskItem, etc).

 

So, for example let's create new Task. All you have to do is pass OlItemType.olTaskItem as an argument to Application.CreateItem ():

 

Outlook.TaskItem myTask = (Outlook.TaskItem ) outlookApp.CreateItem(Outlook.OlItemType.olTaskItem);

myTask.Subject = "Task, created in C#";

myTask.Body = "This is the body of Task item created in C#";

myTask.DueDate = DateTime.Now;

myTask.Save ();

 

With above code you successfully created new very simple Task in Outlook 2003 (you can also add reminders, set more properties (priority, startDate, sensitivity, etc) and implement event handlers (read, reply, open, forward, etc)).


With same approach you can create different types of items.

 

How to display the list of items ?

 

Let's say we want to display a list of all Task. The basic process (regardless of an Outlook items) is:

 

1. Obtain a NameSpace from Application.GetNamespace ()

2. Obtain a MAPIFolder type frome NameSpace.GetDefaultFolder ()

3. Enumerate over the sub-items using the MAPIFolder.Items indexer.

 

With GetNamespace () you will obtain access to data store items. In Outlook 2003 the only namespace you can use is "MAPI". This gives you access to the set of Outlook folders (Inbox, Notes, etc). All folders are represented with OlDefaultFolders enumeration.

 

To request a folder you'll need to pass and value from OlDefaultFolders as an argument to NameSpace.GetDefaultFolder (). Once you have a MAPIFolder type you can loop through the items and use them. Here is the code to display all completed tasks:

 

// Create a namespace

Outlook.NameSpace ns = outlookApp.GetNamespace ("MAPI");

// Get all tasks

Outlook.MAPIFolder myTasks = ns.GetDefaultFolder (Outlook.OlDefaultFolders.olFolderTasks);

foreach (Outlook.TaskItem task in myTasks.Items)

{

      if (task.Complete)

      {   // Display the creation time and body

         string str = task.CreationTime + ": " + task.Body; 

         MessageBox.Show (str); 

      }

}

 

More info on Office/Outlook programming:


 
 
 

转载于:https://www.cnblogs.com/bluewelkin/archive/2008/08/21/1273390.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值