Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
developer
active.documents
automation
binary.file_format
clipboard.dde
com.add_ins
hosting.controls
internet_other
office.sdks
officedev
officedev.other
outlook.forms
outlook.vba
smarttags
vba
web.components
  
 
date: Mon, 7 Jul 2008 00:17:02 -0700,    group: microsoft.public.officedev        back       


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

RE: Adding a toolbar button to PowerPoint with C#   
Doesn't anyone have any suggestions?
date: Thu, 17 Jul 2008 05:59:02 -0700   author:   vdev

RE: Adding a toolbar button to PowerPoint with C#   
I got the button in toolbar working when I added another button to the slides 
(a gray button which would laund the same macro). I think that the other 
button will activate the macros. Don't know how but it works.
date: Wed, 30 Jul 2008 03:56:01 -0700   author:   vdev

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us