hi all,
I am writing a add-in for Outlook 2007 using C# and VSTO ( Visual Studio 2008). When the user clicks on each mailitem in the inbox (or any folder) in the active explorer window, the add-in needs to parse the mail and look for "pdf2:xxxx" and create and add as many commandbarbutto ns to a CommandBar.
The problem is when the I click on the same mail again the eventhandler for the CommandBarButto n gets added again. As a result when the CommandBarButto n is clicked the event is called multiple times.
I test the code by sending a mail with many repetitions of "pdf2:". Is my approach wrong ? how to fix this bug ?
Thanks
arnott
Here is the code :
using System;
using System.Collecti ons.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq ;
using Outlook = Microsoft.Offic e.Interop.Outlo ok;
using Office = Microsoft.Offic e.Core;
using System.Windows. Forms;
namespace OutlookAddIn14
{
public partial class ThisAddIn
{
Outlook.Explore r explorer = null;
Office.CommandB ar docBar = null;
Office.CommandB arPopup newMenuBar;
private string menuTag = "plugintest 14";
//Office.CommandB arButton[] btns;
System.Collecti ons.ArrayList selectedItems = new
System.Collecti ons.ArrayList() ;
System.Collecti ons.ArrayList pdf2Items = new System.Collecti ons.ArrayList() ;
private void ThisAddIn_Start up(object sender, System.EventArg s e)
{
explorer = this.Applicatio n.Explorers.App lication.Active Explorer();
explorer.Select ionChange += new Outlook.
ExplorerEvents_ 10_SelectionCha ngeEventHandler
(explorer_Selec tionChange);
docBar = explorer.Comman dBars.Add(
"Pdf2 Docs", missing, missing, true);
}
private void ThisAddIn_Shutd own(object sender, System.EventArg s e)
{
//RemoveMenubar() ;
}
void explorer_Select ionChange()
{
selectedItems.C lear();
pdf2Items.Clear ();
foreach (object selectedItem in explorer.Select ion)
{
Outlook.MailIte m mailItem = selectedItem as Outlook.MailIte m;
Office.CommandB arButton[] btns;
if (mailItem != null)
{
String[] mainStr;
string[] docStr;
char[] delim = { ' ', '\n', ',' };
char[] dlm = { ':', '/'};
mainStr = mailItem.Body.S plit(delim, System.StringSp litOptions.Remo veEmptyEntries) ;
foreach (string mt in mainStr)
{
if (mt.StartsWith( "pdf2:"))
{
docStr = mt.Split(dlm, System.StringSp litOptions.Remo veEmptyEntries) ;
pdf2Items.Add(d ocStr[docStr.Length-1]);
}
}
//MessageBox.Show (String.Format( "mlink Count : {0} ", pdf2Items.Count ));
if (pdf2Items.Coun t == 0)
{
docBar.Visible = false;
}
if (docBar != null && pdf2Items.Count != 0)
{
try
{
docBar.Visible = false;
docBar.Delete() ;
docBar = explorer.Comman dBars.Add(
"Pdf2 Docs", missing, missing, true);
}
catch (Exception ex)
{
MessageBox.Show (ex.Message);
}
int btnCnt = 0;
btns = new Office.CommandB arButton[pdf2Items.Count];
//btns.Initialize ();
foreach (string mdoc in pdf2Items)
{
Office.CommandB arButton tBtn;
tBtn = (Office.Command BarButton)docBa r.FindControl(O ffice.MsoContro lType.msoContro lButton, missing, mdoc + " - " + btnCnt, missing, false);
if (tBtn == null)
{
btns[btnCnt] = (Office.Command BarButton)docBa r.Controls.Add(
Office.MsoContr olType.msoContr olButton, missing,
missing, missing, true);
// btns[btnCnt].Click -= new Office._Command BarButtonEvents _ClickEventHand ler(Button_Clic k);
btns[btnCnt].Click += new Office._Command BarButtonEvents _ClickEventHand ler(Button_Clic k);
btns[btnCnt].Caption = mdoc;
btns[btnCnt].Tag = mdoc + " - " + btnCnt;
btns[btnCnt].Style = Office.MsoButto nStyle.msoButto nCaption;
btns[btnCnt].FaceId = btnCnt;
//btns[btnCnt].DescriptionTex t = "Document : " + mdoc;
//btns[btnCnt].TooltipText = "Document : " + mdoc;
}
btnCnt++;
}
docBar.Position = Microsoft.Offic e.Core.MsoBarPo sition.msoBarFl oating;
//docBar.Left = 1000;
docBar.Left = this.Applicatio n.ActiveExplore r().Left + this.Applicatio n.ActiveExplore r().Width - 200;
//docBar.Protecti on = Microsoft.Offic e.Core.MsoBarPr otection.msoBar NoMove;
docBar.Top = this.Applicatio n.ActiveExplore r().Top + 200;
docBar.Width = 5 * btns[0].Width;
docBar.Visible = true;
}
else
{
btns = null;
}
}
}
}
void Button_Click(Of fice.CommandBar Button ctrl, ref bool cancelDefault)
{
MessageBox.Show ("clicked : " + ctrl.Caption);
count++;
}
I am writing a add-in for Outlook 2007 using C# and VSTO ( Visual Studio 2008). When the user clicks on each mailitem in the inbox (or any folder) in the active explorer window, the add-in needs to parse the mail and look for "pdf2:xxxx" and create and add as many commandbarbutto ns to a CommandBar.
The problem is when the I click on the same mail again the eventhandler for the CommandBarButto n gets added again. As a result when the CommandBarButto n is clicked the event is called multiple times.
I test the code by sending a mail with many repetitions of "pdf2:". Is my approach wrong ? how to fix this bug ?
Thanks
arnott
Here is the code :
using System;
using System.Collecti ons.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq ;
using Outlook = Microsoft.Offic e.Interop.Outlo ok;
using Office = Microsoft.Offic e.Core;
using System.Windows. Forms;
namespace OutlookAddIn14
{
public partial class ThisAddIn
{
Outlook.Explore r explorer = null;
Office.CommandB ar docBar = null;
Office.CommandB arPopup newMenuBar;
private string menuTag = "plugintest 14";
//Office.CommandB arButton[] btns;
System.Collecti ons.ArrayList selectedItems = new
System.Collecti ons.ArrayList() ;
System.Collecti ons.ArrayList pdf2Items = new System.Collecti ons.ArrayList() ;
private void ThisAddIn_Start up(object sender, System.EventArg s e)
{
explorer = this.Applicatio n.Explorers.App lication.Active Explorer();
explorer.Select ionChange += new Outlook.
ExplorerEvents_ 10_SelectionCha ngeEventHandler
(explorer_Selec tionChange);
docBar = explorer.Comman dBars.Add(
"Pdf2 Docs", missing, missing, true);
}
private void ThisAddIn_Shutd own(object sender, System.EventArg s e)
{
//RemoveMenubar() ;
}
void explorer_Select ionChange()
{
selectedItems.C lear();
pdf2Items.Clear ();
foreach (object selectedItem in explorer.Select ion)
{
Outlook.MailIte m mailItem = selectedItem as Outlook.MailIte m;
Office.CommandB arButton[] btns;
if (mailItem != null)
{
String[] mainStr;
string[] docStr;
char[] delim = { ' ', '\n', ',' };
char[] dlm = { ':', '/'};
mainStr = mailItem.Body.S plit(delim, System.StringSp litOptions.Remo veEmptyEntries) ;
foreach (string mt in mainStr)
{
if (mt.StartsWith( "pdf2:"))
{
docStr = mt.Split(dlm, System.StringSp litOptions.Remo veEmptyEntries) ;
pdf2Items.Add(d ocStr[docStr.Length-1]);
}
}
//MessageBox.Show (String.Format( "mlink Count : {0} ", pdf2Items.Count ));
if (pdf2Items.Coun t == 0)
{
docBar.Visible = false;
}
if (docBar != null && pdf2Items.Count != 0)
{
try
{
docBar.Visible = false;
docBar.Delete() ;
docBar = explorer.Comman dBars.Add(
"Pdf2 Docs", missing, missing, true);
}
catch (Exception ex)
{
MessageBox.Show (ex.Message);
}
int btnCnt = 0;
btns = new Office.CommandB arButton[pdf2Items.Count];
//btns.Initialize ();
foreach (string mdoc in pdf2Items)
{
Office.CommandB arButton tBtn;
tBtn = (Office.Command BarButton)docBa r.FindControl(O ffice.MsoContro lType.msoContro lButton, missing, mdoc + " - " + btnCnt, missing, false);
if (tBtn == null)
{
btns[btnCnt] = (Office.Command BarButton)docBa r.Controls.Add(
Office.MsoContr olType.msoContr olButton, missing,
missing, missing, true);
// btns[btnCnt].Click -= new Office._Command BarButtonEvents _ClickEventHand ler(Button_Clic k);
btns[btnCnt].Click += new Office._Command BarButtonEvents _ClickEventHand ler(Button_Clic k);
btns[btnCnt].Caption = mdoc;
btns[btnCnt].Tag = mdoc + " - " + btnCnt;
btns[btnCnt].Style = Office.MsoButto nStyle.msoButto nCaption;
btns[btnCnt].FaceId = btnCnt;
//btns[btnCnt].DescriptionTex t = "Document : " + mdoc;
//btns[btnCnt].TooltipText = "Document : " + mdoc;
}
btnCnt++;
}
docBar.Position = Microsoft.Offic e.Core.MsoBarPo sition.msoBarFl oating;
//docBar.Left = 1000;
docBar.Left = this.Applicatio n.ActiveExplore r().Left + this.Applicatio n.ActiveExplore r().Width - 200;
//docBar.Protecti on = Microsoft.Offic e.Core.MsoBarPr otection.msoBar NoMove;
docBar.Top = this.Applicatio n.ActiveExplore r().Top + 200;
docBar.Width = 5 * btns[0].Width;
docBar.Visible = true;
}
else
{
btns = null;
}
}
}
}
void Button_Click(Of fice.CommandBar Button ctrl, ref bool cancelDefault)
{
MessageBox.Show ("clicked : " + ctrl.Caption);
count++;
}
Comment