110,533
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Data;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace OutlookAddin1
{
public static class OutlookTool
{
public static void CreateTask(DataRow row)
{
// Create a new task item.
Outlook.MAPIFolder tasksFolder = Globals.ThisApplication.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks);
Outlook.TaskItem item = (Outlook.TaskItem)tasksFolder.Items.Add(Outlook.OlItemType.olTaskItem);
item.Subject = row["Subject"].ToString();
item.Owner = row["Owner"].ToString();
item.PercentComplete = int.Parse(row["PercentComplete"].ToString());
item.Status = (Outlook.OlTaskStatus)int.Parse(row["Status"].ToString());
item.Save();
}
public static void CreateContact(DataRow row)
{
// Create a new contact item.
Outlook.MAPIFolder contactFolder = Globals.ThisApplication.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
Outlook.ContactItem item = (Outlook.ContactItem)contactFolder.Items.Add(Outlook.OlItemType.olContactItem);
item.FirstName = row["firstName"].ToString();
item.LastName = row["lastName"].ToString();
item.Department = row["department"].ToString();
item.JobTitle = row["jobTitle"].ToString();
item.Email1Address = row["email"].ToString();
item.Save();
}
public static string GetContactsXML()
{
// Get all contacts
Outlook.MAPIFolder contactsFolder = Globals.ThisApplication.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
StringBuilder xml = new StringBuilder("<contacts>", 100);
for(int i=1;i<=contactsFolder.Items.Count;i++)
{
Outlook.ContactItem item = contactsFolder.Items[i] as Outlook.ContactItem;
xml.Append("<contact>");
xml.Append("<firstName>" + item.Subject + "</firstName>");
xml.Append("<lastName>" + item.LastName + "</lastName>");
xml.Append("<department>" + item.Department + "</department>");
xml.Append("<jobTitle>" + item.JobTitle + "</jobTitle>");
xml.Append("<email>" + item.Email1Address + "</email>");
xml.Append("</contact>");
if (i > 50)
break;
}
xml.Append("</contacts>");
return xml.ToString();
}
public static string GetTaskXML()
{
// Get all tasks
Outlook.MAPIFolder tasksFolder = Globals.ThisApplication.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks);
StringBuilder xml = new StringBuilder("<tasks>", 100);
foreach (Outlook.TaskItem item in tasksFolder.Items)
{
xml.Append("<task>");
xml.Append("<subject>" + item.Subject + "</subject>");
xml.Append("<owner>" + item.Owner + "</owner>");
xml.Append("<percentComplete>" + item.PercentComplete.ToString() + "</percentComplete>");
xml.Append("<status>" + ((int)item.Status).ToString() + "</status>");
xml.Append("</task>");
}
xml.Append("</tasks>");
return xml.ToString();
}
public static void CreateAppointmentInCalendar(DataRow row)
{
// Create a new appointment item in calendar.
Outlook.MAPIFolder tasksFolder = Globals.ThisApplication.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
Outlook.AppointmentItem appointmentitem = (Outlook.AppointmentItem)tasksFolder.Items.Add(Outlook.OlItemType.olAppointmentItem);
appointmentitem.Subject = row["subject"].ToString();
appointmentitem.Body = row["body"].ToString();
appointmentitem.Start = DateTime.Parse(row["Start"].ToString());
appointmentitem.End = DateTime.Parse(row["End"].ToString());
appointmentitem.Save();
}
public static string GetAppointmentsXML()
{
Outlook.MAPIFolder appointmentFolder = Globals.ThisApplication.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
StringBuilder xml =new StringBuilder( "<appointments>",100);
foreach (Outlook.AppointmentItem item in appointmentFolder.Items)
{
xml.Append("<appointment>");
xml.Append("<subject>" + item.Subject + "</subject>");
xml.Append("<body>" + item.Body + "</body>");
xml.Append("<start>" + item.Start.ToString() + "</start>");
xml.Append("<end>" + item.End.ToString() + "</end>");
xml.Append("</appointment>");
}
xml.Append("</appointments>");
return xml.ToString();
}
private static ListView GetListView(string title, string[] cols)
{
ListView listView = new ListView();
listView.Dock = DockStyle.Fill;
listView.View = View.Details;
foreach (string col in cols)
{
listView.Columns.Add(col);
}
return listView;
}
}
}