2013년 2월 4일 월요일

vsto Catch new item in sync issues folder event

vsto Catch new item in sync issues folder



Something like this would handle various numbers of subfolders under SyncIssues. It uses a handler class to handle each folder instance's Items.ItemAdd() event. It's just stub code, you need to add your own logic and exception handling.
 
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using System.Runtime.InteropServices;
namespace OutlookAddIn1
{
public partial class ThisAddIn
{
private System.Collections.Generic.List<FolderHandler> _list = null;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Outlook.NameSpace ns = this.Application.GetNamespace("MAPI");
Outlook.Folder fol = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSyncIssues) as Outlook.Folder;
GetFolderToAdd(fol);
Marshal.ReleaseComObject(fol);
fol = null;
Marshal.ReleaseComObject(ns);
ns = null;
}
private void GetFolderToAdd(Outlook.Folder folder)
{
AddFolder(folder);
Outlook.Folders fols = folder.Folders;
if (fols.Count > 0)
{
Outlook.Folder fol = null;
for (int i = 1; i <= fols.Count; i++)
{
fol = fols[i] as Outlook.Folder;
GetFolderToAdd(fol);
Marshal.ReleaseComObject(fol);
fol = null;
}
}
Marshal.ReleaseComObject(fols);
fols = null;
}
private void AddFolder(Outlook.Folder folder)
{
_list.Add(new FolderHandler(folder));
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
// handler class:
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Runtime.InteropServices;
namespace OutlookAddIn1
{
public class FolderHandler
{
private Outlook.Folder _folder = null;
private Outlook.Items _items = null;
public FolderHandler(Outlook.Folder folder)
{
_folder = folder;
_items = _folder.Items;
_items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(_items_ItemAdd);
}
public void Kill()
{
_items.ItemAdd -= new Outlook.ItemsEvents_ItemAddEventHandler(_items_ItemAdd);
Marshal.ReleaseComObject(_items);
_items = null;
Marshal.ReleaseComObject(_folder);
_folder = null;
GC.Collect();
}
private void _items_ItemAdd(object Item)
{
throw new NotImplementedException();
}
}
}
 

--
Ken Slovak
MVP - Outlook
http://www.slovaktech.com
Author: Professional Programming Outlook 2007






http://social.msdn.microsoft.com/Forums/en-AU/vsto/thread/28d91a1a-34ec-476d-8f49-0d26aff8a376

댓글 없음: