index > Visual Studio Tools for Office > How to prevent a new toolbar button from being created everytime ...

How to prevent a new toolbar button from being created everytime ...


Hi,

I'm coding a add-in for Outlook in C#. Below is a part of my code. It might be hard to read because of the limited width here but it's not that much code. Questions below:

private void ThisApplication_Startup(object sender, System.EventArgs e)
{
CreateButton();
}

private void CreateButton()
{
Office.CommandBars commandBars = this.ActiveExplorer().CommandBars;

// Create a toolbar button on the standard toolbar that calls ToolbarButton_Click when clicked
try
{
// See if it already exists
toolbarButton = (Office.CommandBarButton)commandBars["Standard"].Controls["ExportContacts"];
}
catch (Exception)
{
// Create it
toolbarButton = (Office.CommandBarButton)commandBars["Standard"].Controls.Add(1, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
toolbarButton.Caption = "Export Contacts";
toolbarButton.TooltipText = "Export your contacts";
toolbarButton.FaceId = 2519;
toolbarButton.Style = Office.MsoButtonStyle.msoButtonIconAndCaption;
}

toolbarButton.Visible = true;
toolbarButton.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.OnToolbarButtonClick);
}


As you see here all I do in ThisApplication_Startup is to call createButton which creates a toolbar button. My problem is that a new button is created everytime Outlook starts. I assume my try/catch-logic isn't doing what I want. Any ideas on how to make it check whether the button already exists before creating a new one?

Cheers
Magnus



ripern
ripern

Hi Magnus

It's not recommended to use try-catch for something that's not a true exception (error). While this worked OK in the old Visual Basic world, in the .NET world this will slow your application considerably. Plus, the way you've set this up, creating the toolbar will execute even if some other error occurs.

Are you certain that the line of code in the "try" block actually causes an error to occur? Might it not leave the toolbarButton variable = null? (no time to test it for you...)

It would probably be better to use the FindControl method of the CommandBars collection. Make sure you've assigned your control a unique TAG value, and check for the control using this (instead of ID, which is only for built-in controls).




-- Cindy Meister (Word MVP)
Cindy Meister
Thanks, I rewrote it entirely and thought I would post it here so others can look at it. It works like I wanted it to do. Something that could be improved though is to not create the button if it already exists but then it's click event didn't work instead so I delete it and then create it. I bit overhead but at least it works and probably only 0.5secs overhead or less. Maybe even better is to remove the button in ThisApplication_Shutdown(...), hmm.

private void ThisApplication_Startup(object sender, System.EventArgs e)
{
AddToolbarButton();
}

private void AddToolbarButton()
{
try
{
// Get a reference to the standard command bar
commandBarStandard = this.ActiveExplorer().CommandBars["Standard"];

// Check whether the "Export Contacts"-button already exists
Office.CommandBarButton foundButton = (Office.CommandBarButton)
this.ActiveExplorer().CommandBars["Standard"].
FindControl(Office.MsoControlType.msoControlButton,
System.Type.Missing, "Export Contacts", true, true);

// If the button exists we delete it (if we just leave it, it won't work like it should)
if (foundButton != null)
foundButton.Delete(true);

// Create the "Export Contacts"-button
buttonExportContacts = (Office.CommandBarButton)commandBarStandard.Controls.Add(1, missing, missing, missing, missing);
buttonExportContacts.Style = Office.MsoButtonStyle.msoButtonIconAndCaption;
buttonExportContacts.Caption = "Export Contacts";
buttonExportContacts.TooltipText = "Export your contacts to IA";
buttonExportContacts.Tag = "Export Contacts";
buttonExportContacts.FaceId = 2519;
buttonExportContacts.Click += new Office._CommandBarButtonEvents_ClickEventHandler(OnButtonClick);

commandBarStandard.Visible = true;
}
catch (ArgumentException e)
{
MessageBox.Show(e.Message);
}
}


ripern
ripern

<<Maybe even better is to remove the button in ThisApplication_Shutdown(...), hmm>>

Yes, if there's any chance the user would uninstall your Add-in :-)




-- Cindy Meister (Word MVP)
Cindy Meister
reply 4

You can use google to search for other answers

 

More Articles

How to add a reference to a DLL made with VS2005 (Framework 2.0) ...
making worksheet read only
Shared AddIn Deployment problem
how can I create activity associations in outlook from within a V...
how to set the cursor at the end of the bookmark?
Dermining if "Out Of Office" is on
Reading Excel worksheet from c#
Context Menus in MS Project
C#, Word VSTO and OnAction (oh my)
Using VSTO to create Excel chart problem..
Welcome to Bokebb   New Update   Joins the collection  
 

New Articles

Is this possible with VSTO 2005?
Problem in Reading Word File Programmati…
Deploying an XML Schema Definition with …
boilerplate text/Autotext
Draw my own squigglies on Word document...
Excel Addin CommandBarButton Click event…
Code checking & code generation in V…
How to: Interact with Windows Forms (C# …
Excel - pivoted field accessUrgent :
Outlook 2003's NewMailEx event handling …
Deplyment of VSTO project with proper se…
InfoPath.FormControl Scrollbar Issue
VSTO WORD delete selected table
VSTO and Office 2003
Using VC++ to control Visio

Hot Articles

VSTO 2005 Outlook Add-in - trying to und…
Deploying a shimmed Excel add-in
Outlook 2003 Addin doesn't work on Windo…
Create a "Bare" Workbook From …
VSTO, Outlook 2003, vs2005 open build-in…
Deploying UDFs
VSTO SE2005 CustomTaskPane Samples...
Problem in Reading Word File Programmati…
Error debuging Excel Smart Doc VSTO 2005
VSTO Question: Are ActionsPanes objects …
Accelerate Smart Tag Recogntion
Hands-on-Lab 04
ItemAdd Event Problem
Microsoft? Office Access 2003 Developer …
Can you get an excel range object from t…

Recommend Articles

VSTO 2005 now supports Outlook!
How to (programmatically )get rid from t…
Excel PIAs
Installing an outlook Addin on another m…
Cypress and task panes accross multiple …
VSTO 2005 standard form question + upgra…
How to determine current XMLNode in a XM…
Unknown Error in DOM when setting node.p…
Getting the Character Style for a word
MDX query to filter the cube
How can I use the Page interface in the …
DataBindings with Outlook Item UserPrope…
Binding ArrayList or List(Of type) types…
howto import from datagridview to excel …
Office SP1 & SP2