我写了这段代码,我有两个问题:
非常感谢帮助。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void GetAllCalendarItems()
{
try
{
// outlook application object
Outlook.Application oApp = new Outlook.Application();
//folder in outlook
Outlook.Folders OutlookFolders = oApp.Session.Folders;
//Mapi folder object
Outlook.MAPIFolder Folder = null;
// outlook exploer object
Outlook.Explorer explorer = null;
//Map subfolder object
Outlook.MAPIFolder SubFolder = null;
// folder int control
int Folderctr;
// sub folder control
int subFolderctr;
// initialize base objects
//date range to get appointments
DateTime start = DateTime.Now;
DateTime end = start.AddDays(5);
// loop throught the PST files added on Outlook
for (Folderctr = 1; Folderctr <= OutlookFolders.Count; Folderctr++)
{
Folder = OutlookFolders[Folderctr];
Console.WriteLine("first name of folder " +Folder.Name);
explorer = Folder.GetExplorer();
//loop throught the folders in the PST files
for (subFolderctr = 1; subFolderctr <= explorer.CurrentFolder.Folders.Count; subFolderctr++)
{
SubFolder = explorer.CurrentFolder.Folders[subFolderctr];
if (SubFolder.Name == "SCE_Calendar")
{
GetAllCalendarItems(SubFolder);
Console.WriteLine("subfolder name of folder " + SubFolder.Name);
}
}
}
//close applciation
oApp.Quit();
// release com object
System.Runtime.InteropServices.Marshal.ReleaseComObject(oApp);
oApp = null;
}
//Simple error handling.
catch (Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
}
}
public void GetAllCalendarItems(Outlook.MAPIFolder seCal)
{
Outlook.Items outlookCalendarItems = null;
//set recursion to true
outlookCalendarItems = seCal.Items;
outlookCalendarItems.IncludeRecurrences = true;
DateTime last;
DateTime first;
foreach (Outlook.AppointmentItem item in outlookCalendarItems)
{
Outlook.RecurrencePattern rp = item.GetRecurrencePattern();
first = new DateTime(2012, 2, 1, item.Start.Hour, item.Start.Minute, 0);
last = new DateTime(2012, 2, 29);
Outlook.AppointmentItem recur = null;
for (DateTime cur = first; cur <= last; cur = cur.AddDays(1))
{
try
{
recur = rp.GetOccurrence(cur);
//Console.WriteLine(recur.Subject + " -> " + cur.ToLongDateString());
Console.WriteLine("Subject: " + recur.Body);
Console.WriteLine("Organizer: " + recur.Organizer);
Console.WriteLine("Start: " + recur.Start.ToString());
Console.WriteLine("End: " + recur.End.ToString());
Console.WriteLine("Location: " + recur.Location);
Console.WriteLine("Recurring: " + recur.IsRecurring);
Console.WriteLine("-----------------------------------------");
Console.WriteLine(" ");
//Show the item to pause.
}
catch
{ }
}
}
}
private void button1_Click(object sender, EventArgs e)
{
GetAllCalendarItems();
}
}
}
答案 0 :(得分:1)
这是我用来获取Outlook约会的代码。我已经把它从一个工作程序中删除了,所以我可能会错过一个辅助函数,但这应该有助于你开始。
private List<Outlook.AppointmentItem> GetOutlookAppointments(DateTime start, DateTime end) {
var outlook = new Microsoft.Office.Interop.Outlook.Application();
var app = outlook.Application;
var appSession = app.Session;
var calendar = appSession.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
string filter = String.Format("[Start] >= {0} And [End] < {1}", start.ToString("ddddd h:nn AMPM"), end.ToString("ddddd h:nn AMPM"));
var items = calendar.Items;
items.Restrict(filter);
items.IncludeRecurrences = true;
Outlook.AppointmentItem appointment;
var appointments = new List<Outlook.AppointmentItem>();
foreach (var item in items)
{
appointment = item as Outlook.AppointmentItem;
if (appointment != null && !appointment.AllDayEvent && (appointment.BusyStatus != Outlook.OlBusyStatus.olOutOfOffice))
{
if (appointment.IsRecurring)
{
AddReoccuringAppointments(appointments, start, end, appointment);
}
else if (appointment.Start >= start && appointment.End < end)
{
appointments.Add(appointment);
}
}
}
}
private static void AddReoccuringAppointments(List<Outlook.AppointmentItem> appointments, DateTime start, DateTime end, Outlook.AppointmentItem appointment)
{
Outlook.RecurrencePattern rp = appointment.GetRecurrencePattern();
if (rp.PatternStartDate >= end || rp.PatternEndDate <= start) { return; }
if (rp.PatternStartDate > start)
{
start = rp.PatternStartDate;
}
if (rp.PatternEndDate < end)
{
end = rp.PatternEndDate;
}
var exceptions = GetExceptions(rp, start, end);
Outlook.AppointmentItem recur = null;
Outlook.Exception exception = null;
for (DateTime cur = new DateTime(start.Year, start.Month, start.Day, appointment.Start.Hour, appointment.Start.Minute, 0); cur <= end; cur = cur.AddDays(1))
{
if ((((int)rp.DayOfWeekMask) & (int)Math.Pow(2,(int)cur.DayOfWeek)) == 0) { continue; }
exception = exceptions.FirstOrDefault(e => e.OriginalDate.Date == cur.Date);
if (exception == null)
{
recur = rp.GetOccurrence(cur);
}
else if (!exception.Deleted)
{
recur = exception.AppointmentItem;
}
if (recur != null)
{
appointments.Add(recur);
}
}
}
private static List<Outlook.Exception> GetExceptions(Outlook.RecurrencePattern rp, DateTime start, DateTime end)
{
List<Outlook.Exception> exceptions = new List<Outlook.Exception>();
foreach (Outlook.Exception e in rp.Exceptions)
{
if (e.OriginalDate >= start && e.OriginalDate < end)
{
exceptions.Add(e);
}
}
return exceptions;
}