Adding a toolbar button to PowerPoint with C#
Hi.
I have an application made with C# which adds a custom toolbar button to
Microsoft Word and PowerPoint. The button works as expected in Word: it
starts a macro when I click the button. But in Powerpoint, clicking the
button doesn't do anything. It should open a macro with the same name as in
Word. Strange thing is that the button starts to work if I just go to Visual
Basic Editor and come back. Below is the code I use to add the button to
PowerPoint (a bit shortened).
===================
private const string START_MACRO = "start";
private const string BUTTON_CAPTION = "Template";
private const string COMMAND_BAR_NAME = "myBar";
// Open PowerPoint
PPApp = new PowerPoint.ApplicationClass();
PPAppPresSet = PPApp.Presentations;
// PPApp must be set visible before modifying the presentation!
PPApp.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
// Open presentation based on blank.pot template
PPAppPres = PPAppPresSet.Open(fileNamePP,
Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoTrue,
Microsoft.Office.Core.MsoTriState.msoTrue);
// For PowerPoint
if (PPApp != null && !this.CommandBarExistsInPowerPoint(PPApp,
COMMAND_BAR_NAME))
{
oCommandBarPP = AddPowerPointToolbar(PPApp, COMMAND_BAR_NAME);
// Create a new toolbar and show it to the user.
oCommandBarPP.Visible = true;
oCommandBarPP.Position = Microsoft.Office.Core.MsoBarPosition.msoBarTop |
Microsoft.Office.Core.MsoBarPosition.msoBarLeft;
oCommandBarButtonPP = (Microsoft.Office.Core.CommandBarButton)
oCommandBarPP.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton, missing, missing, missing, missing);
// Assign a macro to the button.
oCommandBarButtonPP.OnAction = START_MACRO;
// Set the caption of the button.
oCommandBarButtonPP.Caption = BUTTON_CAPTION;
oCommandBarButtonPP.Style =
Microsoft.Office.Core.MsoButtonStyle.msoButtonCaption;
}
/// <summary>
/// Tells if the given toolbar name exists in the PowerPoint application
/// </summary>
private bool CommandBarExistsInPowerPoint(
PowerPoint.Application ppApp, string toolbarName)
{
//check if valuatum command bar already exist
for (int i =1; i<=ppApp.CommandBars.Count; i++)
{
if (ppApp.CommandBars[i].Name == toolbarName)
{
return true;
}
}
return false;
}
/// <summary>
/// Adds a toolbar to the powerpoint application
/// </summary>
private Microsoft.Office.Core.CommandBar AddPowerPointToolbar(
PowerPoint.Application ppApp, string toolbarName)
{
Microsoft.Office.Core.CommandBar toolBar = null;
try
{
// Create a command bar for the add-in
object missing = System.Reflection.Missing.Value;
toolBar = (Microsoft.Office.Core.CommandBar)ppApp.CommandBars.Add(toolbarName,
missing, missing, false);
toolBar.Visible = true;
return toolBar;
}
catch
{
// Add exception handling here.
return null;
}
}
===================
The code for adding a toolbar button to Word is exactly the same but just
replace PowerPoint.ApplicationClass with Word.ApplicationClass .
So any ideas where the problem could be? Why is the button working in Word
but not in PowerPoint? Is there something missing in my code?
date: Mon, 7 Jul 2008 00:17:02 -0700
author: vdev