项目里面添加上Microsoft.Office.Interop.Outlook引用。下面的代码只是演示获取开始时间,结束时间和地点。其他的信息根据需要自己添加一下,看看可不可以。
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using Outlook = Microsoft.Office.Interop.Outlook;namespace OutlookAppointment
{
/// <summary>
/// Description of MainForm.
/// </summary>
public class MainForm : Form
{
/// <summary>
/// Designer variable used to keep track of non-visual components.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Disposes resources used by the form.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing) {
if (components != null) {
components.Dispose();
}
}
base.Dispose(disposing);
}
/// <summary>
/// This method is required for Windows Forms designer support.
/// Do not change the method contents inside the source code editor. The Forms designer might
/// not be able to load this method if it was changed manually.
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
this.dateTimePicker2 = new System.Windows.Forms.DateTimePicker();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.textBox2 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(110, 13);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(200, 21);
this.textBox1.TabIndex = 0;
//
// dateTimePicker1
//
this.dateTimePicker1.Location = new System.Drawing.Point(110, 38);
this.dateTimePicker1.Name = "dateTimePicker1";
this.dateTimePicker1.Size = new System.Drawing.Size(200, 21);
this.dateTimePicker1.TabIndex = 1;
this.dateTimePicker1.Value = new System.DateTime(2014, 1, 1, 17, 3, 0, 0);
//
// dateTimePicker2
//
this.dateTimePicker2.Location = new System.Drawing.Point(110, 69);
this.dateTimePicker2.Name = "dateTimePicker2";
this.dateTimePicker2.Size = new System.Drawing.Size(200, 21);
this.dateTimePicker2.TabIndex = 2;
//
// label1
//
this.label1.Location = new System.Drawing.Point(4, 13);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(100, 23);
this.label1.TabIndex = 3;
this.label1.Text = "To";
//
// label2
//
this.label2.Location = new System.Drawing.Point(4, 38);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(100, 23);
this.label2.TabIndex = 4;
this.label2.Text = "Start Time";
//
// label3
//
this.label3.Location = new System.Drawing.Point(4, 69);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(100, 23);
this.label3.TabIndex = 5;
this.label3.Text = "End Time";
//
// button1
//
this.button1.Location = new System.Drawing.Point(110, 95);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(206, 23);
this.button1.TabIndex = 6;
this.button1.Text = "Get Info";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.Button1Click);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(4, 125);
this.textBox2.Multiline = true;
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(603, 202);
this.textBox2.TabIndex = 7;
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(609, 331);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.button1);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.dateTimePicker2);
this.Controls.Add(this.dateTimePicker1);
this.Controls.Add(this.textBox1);
this.Name = "MainForm";
this.Text = "OutlookAppointment";
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.DateTimePicker dateTimePicker2;
private System.Windows.Forms.DateTimePicker dateTimePicker1;
private System.Windows.Forms.TextBox textBox1;
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
private DateTime start;
private DateTime end;
void Button1Click(object sender, EventArgs e)
{
start = dateTimePicker1.Value;
end = dateTimePicker2.Value;
DemoAppointmentsInRange();
} private void DemoAppointmentsInRange()
{
Outlook.Application app = new Outlook.Application();
Outlook.Folder calFolder =
app.Session.GetDefaultFolder(
Outlook.OlDefaultFolders.olFolderCalendar)
as Outlook.Folder;
Outlook.Items rangeAppts = GetAppointmentsInRange(calFolder, start, end);
if (rangeAppts != null)
{
foreach (Outlook.AppointmentItem appt in rangeAppts)
{
textBox2.Text += (
"Subject: " + appt.Subject
+ " Start: " + appt.Start.ToString("g")
+ " Location: " + appt.Location
);
}
}
} /// <summary>
/// Get recurring appointments in date range.
/// </summary>
/// <param name="folder"></param>
/// <param name="startTime"></param>
/// <param name="endTime"></param>
/// <returns>Outlook.Items</returns>
private Outlook.Items GetAppointmentsInRange(
Outlook.Folder folder, DateTime startTime, DateTime endTime)
{
string filter = "[Start] >= '"
+ startTime.ToString("g")
+ "' AND [End] <= '"
+ endTime.ToString("g") + "'";
Debug.WriteLine(filter);
try
{
Outlook.Items calItems = folder.Items;
calItems.IncludeRecurrences = true;
calItems.Sort("[Start]", Type.Missing);
Outlook.Items restrictItems = calItems.Restrict(filter);
if (restrictItems.Count > 0)
{
return restrictItems;
}
else
{
return null;
}
}
catch { return null; }
}
}
}
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using Outlook = Microsoft.Office.Interop.Outlook;namespace OutlookAppointment
{
/// <summary>
/// Description of MainForm.
/// </summary>
public class MainForm : Form
{
/// <summary>
/// Designer variable used to keep track of non-visual components.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Disposes resources used by the form.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing) {
if (components != null) {
components.Dispose();
}
}
base.Dispose(disposing);
}
/// <summary>
/// This method is required for Windows Forms designer support.
/// Do not change the method contents inside the source code editor. The Forms designer might
/// not be able to load this method if it was changed manually.
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
this.dateTimePicker2 = new System.Windows.Forms.DateTimePicker();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.textBox2 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(110, 13);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(200, 21);
this.textBox1.TabIndex = 0;
//
// dateTimePicker1
//
this.dateTimePicker1.Location = new System.Drawing.Point(110, 38);
this.dateTimePicker1.Name = "dateTimePicker1";
this.dateTimePicker1.Size = new System.Drawing.Size(200, 21);
this.dateTimePicker1.TabIndex = 1;
this.dateTimePicker1.Value = new System.DateTime(2014, 1, 1, 17, 3, 0, 0);
//
// dateTimePicker2
//
this.dateTimePicker2.Location = new System.Drawing.Point(110, 69);
this.dateTimePicker2.Name = "dateTimePicker2";
this.dateTimePicker2.Size = new System.Drawing.Size(200, 21);
this.dateTimePicker2.TabIndex = 2;
//
// label1
//
this.label1.Location = new System.Drawing.Point(4, 13);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(100, 23);
this.label1.TabIndex = 3;
this.label1.Text = "To";
//
// label2
//
this.label2.Location = new System.Drawing.Point(4, 38);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(100, 23);
this.label2.TabIndex = 4;
this.label2.Text = "Start Time";
//
// label3
//
this.label3.Location = new System.Drawing.Point(4, 69);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(100, 23);
this.label3.TabIndex = 5;
this.label3.Text = "End Time";
//
// button1
//
this.button1.Location = new System.Drawing.Point(110, 95);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(206, 23);
this.button1.TabIndex = 6;
this.button1.Text = "Get Info";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.Button1Click);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(4, 125);
this.textBox2.Multiline = true;
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(603, 202);
this.textBox2.TabIndex = 7;
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(609, 331);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.button1);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.dateTimePicker2);
this.Controls.Add(this.dateTimePicker1);
this.Controls.Add(this.textBox1);
this.Name = "MainForm";
this.Text = "OutlookAppointment";
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.DateTimePicker dateTimePicker2;
private System.Windows.Forms.DateTimePicker dateTimePicker1;
private System.Windows.Forms.TextBox textBox1;
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
private DateTime start;
private DateTime end;
void Button1Click(object sender, EventArgs e)
{
start = dateTimePicker1.Value;
end = dateTimePicker2.Value;
DemoAppointmentsInRange();
} private void DemoAppointmentsInRange()
{
Outlook.Application app = new Outlook.Application();
Outlook.Folder calFolder =
app.Session.GetDefaultFolder(
Outlook.OlDefaultFolders.olFolderCalendar)
as Outlook.Folder;
Outlook.Items rangeAppts = GetAppointmentsInRange(calFolder, start, end);
if (rangeAppts != null)
{
foreach (Outlook.AppointmentItem appt in rangeAppts)
{
textBox2.Text += (
"Subject: " + appt.Subject
+ " Start: " + appt.Start.ToString("g")
+ " Location: " + appt.Location
);
}
}
} /// <summary>
/// Get recurring appointments in date range.
/// </summary>
/// <param name="folder"></param>
/// <param name="startTime"></param>
/// <param name="endTime"></param>
/// <returns>Outlook.Items</returns>
private Outlook.Items GetAppointmentsInRange(
Outlook.Folder folder, DateTime startTime, DateTime endTime)
{
string filter = "[Start] >= '"
+ startTime.ToString("g")
+ "' AND [End] <= '"
+ endTime.ToString("g") + "'";
Debug.WriteLine(filter);
try
{
Outlook.Items calItems = folder.Items;
calItems.IncludeRecurrences = true;
calItems.Sort("[Start]", Type.Missing);
Outlook.Items restrictItems = calItems.Restrict(filter);
if (restrictItems.Count > 0)
{
return restrictItems;
}
else
{
return null;
}
}
catch { return null; }
}
}
}
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using Outlook = Microsoft.Office.Interop.Outlook;namespace OutlookAppointment
{
/// <summary>
/// Description of MainForm.
/// </summary>
public class MainForm : Form
{
/// <summary>
/// Designer variable used to keep track of non-visual components.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Disposes resources used by the form.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing) {
if (components != null) {
components.Dispose();
}
}
base.Dispose(disposing);
}
/// <summary>
/// This method is required for Windows Forms designer support.
/// Do not change the method contents inside the source code editor. The Forms designer might
/// not be able to load this method if it was changed manually.
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
this.dateTimePicker2 = new System.Windows.Forms.DateTimePicker();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.textBox2 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(110, 13);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(200, 21);
this.textBox1.TabIndex = 0;
//
// dateTimePicker1
//
this.dateTimePicker1.Location = new System.Drawing.Point(110, 38);
this.dateTimePicker1.Name = "dateTimePicker1";
this.dateTimePicker1.Size = new System.Drawing.Size(200, 21);
this.dateTimePicker1.TabIndex = 1;
this.dateTimePicker1.Value = new System.DateTime(2014, 1, 1, 17, 3, 0, 0);
//
// dateTimePicker2
//
this.dateTimePicker2.Location = new System.Drawing.Point(110, 69);
this.dateTimePicker2.Name = "dateTimePicker2";
this.dateTimePicker2.Size = new System.Drawing.Size(200, 21);
this.dateTimePicker2.TabIndex = 2;
//
// label1
//
this.label1.Location = new System.Drawing.Point(4, 13);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(100, 23);
this.label1.TabIndex = 3;
this.label1.Text = "To";
//
// label2
//
this.label2.Location = new System.Drawing.Point(4, 38);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(100, 23);
this.label2.TabIndex = 4;
this.label2.Text = "Start Time";
//
// label3
//
this.label3.Location = new System.Drawing.Point(4, 69);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(100, 23);
this.label3.TabIndex = 5;
this.label3.Text = "End Time";
//
// button1
//
this.button1.Location = new System.Drawing.Point(110, 95);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(206, 23);
this.button1.TabIndex = 6;
this.button1.Text = "Get Info";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.Button1Click);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(4, 125);
this.textBox2.Multiline = true;
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(603, 202);
this.textBox2.TabIndex = 7;
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(609, 331);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.button1);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.dateTimePicker2);
this.Controls.Add(this.dateTimePicker1);
this.Controls.Add(this.textBox1);
this.Name = "MainForm";
this.Text = "OutlookAppointment";
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.DateTimePicker dateTimePicker2;
private System.Windows.Forms.DateTimePicker dateTimePicker1;
private System.Windows.Forms.TextBox textBox1;
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
private DateTime start;
private DateTime end;
private String location;
void Button1Click(object sender, EventArgs e)
{
start = dateTimePicker1.Value;
end = dateTimePicker2.Value;
location = textBox1.Text.Trim();
DemoAppointmentsInRange();
} private void DemoAppointmentsInRange()
{
Outlook.Application app = new Outlook.Application();
Outlook.Folder calFolder =
app.Session.GetDefaultFolder(
Outlook.OlDefaultFolders.olFolderCalendar)
as Outlook.Folder; Outlook.Items rangeAppts = GetAppointmentsInRange(calFolder, start, end);
if (rangeAppts != null)
{
foreach (Outlook.AppointmentItem appt in rangeAppts)
{
textBox2.Text += (
"Subject: " + appt.Subject
+ " Start: " + appt.Start.ToString("g")
//+ " End: " + appt.End.ToString("g")
+ " Location: " + appt.Location
//+ " MeetingStatus: " + appt.MeetingStatus
);
}
}
} /// <summary>
/// Get recurring appointments in date range.
/// </summary>
/// <param name="folder"></param>
/// <param name="startTime"></param>
/// <param name="endTime"></param>
/// <returns>Outlook.Items</returns>
private Outlook.Items GetAppointmentsInRange(
Outlook.Folder folder, DateTime startTime, DateTime endTime)
{
string filter = "[Start] >= '"
+ startTime.ToString("g")
+ "' AND [End] <= '"
+ endTime.ToString("g")
+ "' AND [MeetingLocation] = '"
+ location
+ "'";
Debug.WriteLine(filter);
try
{
Outlook.Items calItems = folder.Items;
calItems.IncludeRecurrences = true;
calItems.Sort("[Start]", Type.Missing);
Outlook.Items restrictItems = calItems.Restrict(filter);
if (restrictItems.Count > 0)
{
return restrictItems;
}
else
{
return null;
}
}
catch { return null; }
}
}
}