I tested this on an Outlook 2003 SP2 system running a mailbox against
Exchange 2003. I used VSTO 2005.
Using your code I could replicate the problems you reported. I did change
the folder paths to use Tasks1 and Tasks2 as subfolders directly under All
Public Folders, just to simplify things on my public folders.
I then rewrote your code to not release the objects in the startup code,
moved some release code to shutdown and made a few other changes according
to what I felt were best practices. After the changes the problems went
away.
Change the folder paths back to yours and try out my code and see if that
solves your problems. Let us know.
using System;
using System.Windows.Forms;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace Test
{
public partial class ThisApplication
{
public static ThisApplication OlApp;
private void ThisApplication_Startup(object sender, System.EventArgs
e)
{
try
{
OlApp = (ThisApplication)sender;
string s = @"\\Public Folders\All Public Folders";
Outlook.MAPIFolder _folder1 = GetFolder(s + @"\Tasks1");
_fev1 = new FolderEventHandler(_folder1);
//Marshal.ReleaseComObject(folder1);
Outlook.MAPIFolder _folder2 = GetFolder(s + @"\Tasks2");
_fev2 = new FolderEventHandler(_folder2);
//Marshal.ReleaseComObject(folder2);
}
catch (Exception ex)
{
MessageBox.Show(" OutlookAddin3:ThisApplication_Startup
error.\n" + ex.Message);
}
}
private void ThisApplication_Shutdown(object sender,
System.EventArgs e)
{
_fev1.ReleaseHandler();
_fev1 = null;
_fev2.ReleaseHandler();
_fev2 = null;
_folder1 = null;
_folder2 = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
private Outlook.MAPIFolder _folder1, _folder2;
private FolderEventHandler _fev1, _fev2;
public static Outlook.MAPIFolder GetFolder(string folderName)
{
Outlook.Folders folders = null;
Outlook.MAPIFolder folder = null, subFolder = null;
Outlook.NameSpace ns = OlApp.GetNamespace("MAPI");
try
{
folders = ns.Folders;
string[] fld = folderName.Split(new char[] { '\\', '/' },
StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < fld.Length; i++)
{
NAR(folder);
folder = null;
folder = folders[fld
];
NAR(folders);
folders = null;
folders = folder.Folders;
}
subFolder = folder;
folder = null;
}
finally
{
NAR(folder);
NAR(folders);
NAR(ns);
}
return subFolder;
}
public static void NAR(object o)
{
try
{
if (o != null)
Marshal.ReleaseComObject(o);
}
catch { }
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new
System.EventHandler(ThisApplication_Startup);
this.Shutdown += new
System.EventHandler(ThisApplication_Shutdown);
}
#endregion
}
class FolderEventHandler
{
public FolderEventHandler(Outlook.MAPIFolder folder)
{
_folderItems = folder.Items;
_folderItems.ItemChange += new
Outlook.ItemsEvents_ItemChangeEventHandler(_folderItems_ItemChange);
}
void _folderItems_ItemChange(object Item)
{
Debug.Print("_folderItems_ItemChange");
}
private Outlook.Items _folderItems;
public void ReleaseHandler()
{
_folderItems.ItemChange -= new
Outlook.ItemsEvents_ItemChangeEventHandler(_folderItems_ItemChange);
_folderItems = null;
}
}
}
Godandag said:
Sorry, Ken. I did not check that code carefully - it is works IE.
The problem depends on the order, in witch I call GetFolder and
folder.Items for different folders.
This is the new code (nothing is skipped). I had check it on 3
different comps.
It is to public folders (i had test on task folders) required:
\\Public Folders\All Public Folders\Test1\Folder1
\\Public Folders\All Public Folders\Test1\Folder2
To see the problem:
- create some items in both folders
- close Outlook, start Outlook (addin must be loaded)
- go to folder1 and switch to view without groupping and filtering!!!!!
- remove one item in folder1 - items count in status bar not changed!!!
- switch to folder2 and back to folder1 (for refresh folder1 content) -
blank line instead of deleted item!!!
The creating of items in folder1 is also wrong (they not displayed
until Outlooks restart).
All above only for views without groupping and filtering!!!!!
Client - Outlook 2003 SP2, VSTO 2005.
Server - Exchage 2003 on Win2003 SP1
This is the code:
<snip>