Thanks Ken. FYI, here is the VB code adapted to .NET C#. I made a few
changes.
using System;
using System.Text;
using System.Runtime.InteropServices;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace OutlookAddIn
{
public class MailManager
{
private const int WUM_RESETNOTIFICATION = 1031;
private const int NIM_DELETE = 2;
private delegate bool EnumWindowProc( IntPtr hwnd, int i);
[StructLayout(LayoutKind.Sequential)]
internal struct NOTIFYICONDATA
{
public int cbSize;
public IntPtr hwnd;
public int uID;
public int uFlags;
public int uCallbackMessage;
public int hIcon;
public string szTip;
}
[DllImport("user32.dll")]
private static extern int SendMessage( IntPtr hwnd, int msg, int wParam,
int lParam);
[DllImport("user32.dll")]
private static extern int GetClassName(IntPtr hwnd,
[In][Out]StringBuilder lpClassName, int nMaxCount);
[DllImport("user32.dll")]
private static extern int EnumWindows(EnumWindowProc callback,int
lParam);
[DllImport("shell32.dll")]
private static extern int Shell_NotifyIcon(int dwMessage, ref
NOTIFYICONDATA lpData);
public MailManager()
{
}
public void ValidateNewMail(object item)
{
Outlook.MailItem mail = item as Outlook.MailItem;
if( mail != null)
{
if( mail.Subject == "InterScan NT Alert")
{
mail.UnRead = false;
mail.Delete();
RemoveNewMailIcon();
}
else if( mail.Attachments.Count > 0)
{
Outlook.Attachment attachment = mail.Attachments[1] as
Outlook.Attachment;
if( attachment != null)
{
if( attachment.FileName == "InterScan_SafeStamp.txt")
{
mail.UnRead = false;
mail.Delete();
RemoveNewMailIcon();
}
}
}
}
}
private void RemoveNewMailIcon()
{
EnumWindows( new EnumWindowProc(this.EnumWindowCallback), 0);
}
private bool EnumWindowCallback(IntPtr hwnd, int i)
{
bool bReturn = true;
StringBuilder sb = new StringBuilder(64);
GetClassName(hwnd,sb,64);
string sClass = sb.ToString();
if( sClass =="rctrl_renwnd32")
{
if(KillNewMailIcon(hwnd))
{
bReturn = false;
SendMessage(hwnd,WUM_RESETNOTIFICATION,0,0);
}
}
return bReturn;
}
private bool KillNewMailIcon(IntPtr hwnd)
{
bool bReturn= false;
NOTIFYICONDATA nid = new NOTIFYICONDATA();
nid.cbSize =Marshal.SizeOf(typeof(NOTIFYICONDATA));
nid.hwnd = hwnd;
nid.uID = 0;
int hResult;
hResult = Shell_NotifyIcon(NIM_DELETE,ref nid);
if( hResult != 0)
bReturn = true;
return bReturn;
}
}
}
> Look at the Outlook VBA code sample at
> http://www.slipstick.com/dev/code/clearenvicon.htm for how to clear
[quoted text clipped - 53 lines]
> > }
> > }
Anthony Christianson - 20 Nov 2003 17:59 GMT
There is a timing issue though. I am still working on a fix for it.
> Thanks Ken. FYI, here is the VB code adapted to .NET C#. I made a few
> changes.
[quoted text clipped - 176 lines]
> > > }
> > > }
Anthony Christianson - 20 Nov 2003 20:09 GMT
Final working code.
One draw back of this code, if you receive a valid email followed by an
invalid email, this will cause the Icon to be removed.
In the Add-In Class
//MailManager object in the Add-In
private MailManager _manager= new MailManager();
//applicationObject is Microsoft.Office.Interop.Outlook.Application
applicationObject.NewMail += new
Microsoft.Office.Interop.Outlook.ApplicationEvents_10_NewMailEventHandler(ap
plicationObject_NewMail);
//Event Handler for Microsoft.Office.Interop.Outlook.Application.NewMail
event
private void applicationObject_NewMail()
{
_manager.CleanInbox(this.applicationObject);
if(_manager.RemoveIcon)
_manager.RemoveNewMailIcon();
}
//Class to encapsulate Inbox Cleaning
public class MailManager
{
//Function pointer to Callback
private delegate bool EnumWindowProc( IntPtr hwnd, int i);
private bool _removeIcon = false;
[StructLayout(LayoutKind.Sequential)]
internal struct NOTIFYICONDATA
{
public int cbSize;
public IntPtr hwnd;
public int uID;
public int uFlags;
public int uCallbackMessage;
public int hIcon;
public string szTip;
}
[DllImport("user32.dll")]
private static extern int SendMessage( IntPtr hwnd, int msg, int wParam,
int lParam);
[DllImport("user32.dll")]
private static extern int GetClassName(IntPtr hwnd, [In][Out]StringBuilder
lpClassName, int nMaxCount);
[DllImport("user32.dll")]
private static extern int EnumWindows(EnumWindowProc callback,int lParam);
[DllImport("shell32.dll")]
private static extern int Shell_NotifyIcon(int dwMessage, ref
NOTIFYICONDATA lpData);
public MailManager()
{
}
//Property if the mail should clear the Icon
public bool RemoveIcon
{
get{ return _removeIcon;}
set{_removeIcon=value;}
}
public void ValidateNewMail(object item)
{
//Here is where I deteremine whether to keep to toss item.
Outlook.MailItem mail = item as Outlook.MailItem;
_removeIcon=false;
if( mail != null)
{
if( mail.Subject == "InterScan NT Alert")
{
mail.UnRead = false;
mail.Delete();
_removeIcon=true;
}
else if( mail.Attachments.Count > 0)
{
Outlook.Attachment attachment = mail.Attachments[1] as
Outlook.Attachment;
if( attachment != null)
{
if( attachment.FileName == "InterScan_SafeStamp.txt")
{
mail.UnRead = false;
mail.Delete();
_removeIcon=true;
}
}
}
}
}
//Loops through all items in the Inbox. New Items are Validated.
public void CleanInbox(Outlook.Application outlook)
{
Outlook.NameSpace ns = outlook.GetNamespace("MAPI");
Outlook.MAPIFolder inbox =
ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
int i= inbox.Items.Count;
while( i > 0 )
{
try
{
Outlook.MailItem mail = inbox.Items[i] as Outlook.MailItem;
if( mail.UnRead)
ValidateNewMail(mail);
}
catch{}
finally
{
i--;
}
}
}
public void RemoveNewMailIcon()
{
EnumWindows( new EnumWindowProc(this.EnumWindowCallback), 0);
}
private bool EnumWindowCallback(IntPtr hwnd, int i)
{
bool bReturn = true;
StringBuilder sb = new StringBuilder(64);
GetClassName(hwnd,sb,64);
string sClass = sb.ToString();
if( sClass =="rctrl_renwnd32")
{
if(KillNewMailIcon(hwnd))
{
bReturn = false;
SendMessage(hwnd,1031,0,0);
}
}
return bReturn;
}
private bool KillNewMailIcon(IntPtr hwnd)
{
bool bReturn= false;
NOTIFYICONDATA nid = new NOTIFYICONDATA();
nid.cbSize =Marshal.SizeOf(typeof(NOTIFYICONDATA));
nid.hwnd = hwnd;
nid.uID = 0;
int hResult;
hResult = Shell_NotifyIcon(2,ref nid);
if( hResult != 0)
bReturn = true;
return bReturn;
}
}
> There is a timing issue though. I am still working on a fix for it.
>
[quoted text clipped - 179 lines]
> > > > }
> > > > }