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 commandbarbuttons to a CommandBar.
The problem is when the I click on the same mail again the eventhandler for the CommandBarButton gets added again. As a result when the CommandBarButton 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.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using System.Windows.Forms;
namespace OutlookAddIn14
{
public partial class ThisAddIn
{
Outlook.Explorer explorer = null;
Office.CommandBar docBar = null;
Office.CommandBarPopup newMenuBar;
private string menuTag = "plugintest14";
//Office.CommandBarButton[] btns;
System.Collections.ArrayList selectedItems = new
System.Collections.ArrayList();
System.Collections.ArrayList pdf2Items = new System.Collections.ArrayList();
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
explorer = this.Application.Explorers.Application.ActiveExplo rer();
explorer.SelectionChange += new Outlook.
ExplorerEvents_10_SelectionChangeEventHandler
(explorer_SelectionChange);
docBar = explorer.CommandBars.Add(
"Pdf2 Docs", missing, missing, true);
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
//RemoveMenubar();
}
void explorer_SelectionChange()
{
selectedItems.Clear();
pdf2Items.Clear();
foreach (object selectedItem in explorer.Selection)
{
Outlook.MailItem mailItem = selectedItem as Outlook.MailItem;
Office.CommandBarButton[] btns;
if (mailItem != null)
{
String[] mainStr;
string[] docStr;
char[] delim = { ' ', '\n', ',' };
char[] dlm = { ':', '/'};
mainStr = mailItem.Body.Split(delim, System.StringSplitOptions.RemoveEmptyEntries);
foreach (string mt in mainStr)
{
if (mt.StartsWith("pdf2:"))
{
docStr = mt.Split(dlm, System.StringSplitOptions.RemoveEmptyEntries);
pdf2Items.Add(docStr[docStr.Length-1]);
}
}
//MessageBox.Show(String.Format("mlink Count : {0} ", pdf2Items.Count));
if (pdf2Items.Count == 0)
{
docBar.Visible = false;
}
if (docBar != null && pdf2Items.Count != 0)
{
try
{
docBar.Visible = false;
docBar.Delete();
docBar = explorer.CommandBars.Add(
"Pdf2 Docs", missing, missing, true);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
int btnCnt = 0;
btns = new Office.CommandBarButton[pdf2Items.Count];
//btns.Initialize();
foreach (string mdoc in pdf2Items)
{
Office.CommandBarButton tBtn;
tBtn = (Office.CommandBarButton)docBar.FindControl(Office .MsoControlType.msoControlButton, missing, mdoc + " - " + btnCnt, missing, false);
if (tBtn == null)
{
btns[btnCnt] = (Office.CommandBarButton)docBar.Controls.Add(
Office.MsoControlType.msoControlButton, missing,
missing, missing, true);
// btns[btnCnt].Click -= new Office._CommandBarButtonEvents_ClickEventHandler(B utton_Click);
btns[btnCnt].Click += new Office._CommandBarButtonEvents_ClickEventHandler(B utton_Click);
btns[btnCnt].Caption = mdoc;
btns[btnCnt].Tag = mdoc + " - " + btnCnt;
btns[btnCnt].Style = Office.MsoButtonStyle.msoButtonCaption;
btns[btnCnt].FaceId = btnCnt;
//btns[btnCnt].DescriptionText = "Document : " + mdoc;
//btns[btnCnt].TooltipText = "Document : " + mdoc;
}
btnCnt++;
}
docBar.Position = Microsoft.Office.Core.MsoBarPosition.msoBarFloatin g;
//docBar.Left = 1000;
docBar.Left = this.Application.ActiveExplorer().Left + this.Application.ActiveExplorer().Width - 200;
//docBar.Protection = Microsoft.Office.Core.MsoBarProtection.msoBarNoMov e;
docBar.Top = this.Application.ActiveExplorer().Top + 200;
docBar.Width = 5 * btns[0].Width;
docBar.Visible = true;
}
else
{
btns = null;
}
}
}
}
void Button_Click(Office.CommandBarButton ctrl, ref bool cancelDefault)
{
MessageBox.Show("clicked : " + ctrl.Caption);
count++;
}