Answered by:
Click event handler for command button (on the powerpoint slide)

Question
-
Hello,
I have created a command button on the powerpoint slide (PowerPoint.Shape vButton = Sld.Shapes.AddOLEObject(10, 500, 100, 30, "Forms.CommandButton.1", null, Office.MsoTriState.msoFalse, null, 0, null, Office.MsoTriState.msoFalse);). Now my question is
-> how to handle the click event for the command button?
I am using powerpoint 2007, and VSTO 2008. Please help me.
Kind Regards,
BRFriday, May 8, 2009 1:07 PM
Answers
-
Hi,
We have reviewed your code and following is our assesment,
The third shape, and most significant shape added to the slide, is a Microsoft Office VBA Forms2 button named "vButton" which is placed on the slide using the following 2 lines of code:PowerPoint.Shape vButton = Sld.Shapes.AddOLEObject(10, 500, 100, 30, "Forms.CommandButton.1", null, Office.MsoTriState.msoFalse, null, 0, null, Office.MsoTriState.msoFalse);
vButton.Name = "btnVoting";
The C# code successfully places the button on the slide, where it shows click behavior, but does nothing. That is because the button fires only one event - Click. The sink, or listener for that event is in a Forms 2.0 form. Expressed another way, the only object that hears the click is a Forms 2.0 form. There is no way for the PowerPoint add-in to respond to the click of that button.
In previous logs in this thread there have been examples for using the "ActionButton: Custom Action Button" shape and setting its acction to msoActionRunMacro. If the PowerPoint presentation is designed in advance you can add a VBA macro to it that calls a Form built by the Forms 2.0 dll.
You build the form on the editing surface of the VBA IDE. To see this surface you need to have a "Developer" tab in the ribbon at the top of the PowerPoint design screen. To get that, if you don't already have one, click the round "Office" button at the left end of the ribbon. In the dropdown page, at the bottom there is a "PowerPoint Options" button. Click that. The PowerPoint Options dialog opens, with the default page "Popular" showing. Click to check the box beside the line that says "Show Developer tab in the Ribbon", then click the OK button at the bottom to get back to the presentation design screen.
The "Developer" tab will be at the right end of the ribbon. Click it to open the Developer ribbon. At the left end there is a button named "Macros" Click it. In the dialog that opens create a macro named "Test" and click OK. The VBA IDE will open,and show the empty macro that looks like this:
Sub Test()
End Sub
We'll come back to that in a minute, but first you want to design a form. In the VBA IDE menu bar click the "Insert" menu, and select "User Form". The UserForm1 design window will open and a small Toolbox will float beside it. Use those tools to build the form. Also, notice at the left of the screen there is a Property box for the form. You can change properties to tailor the appearance of the form. When you have the form, minimize it and go back to the macro.
Assuming the form is named "UserForm1" the macro can be as simple as Sub Test()
UserForm1.Show
End Sub
Back on the slide the Custom action button's action is to run the macro which calls the form.
The line in the C# code that says oShape.ActionSettings[PowerPoint.PpMouseActivation.ppMouseClick].Run = "Test"; specifically runs that macro.
Alternatively,from an object on the presentation you can call into an add-in to execute a member method. There are several approaches. Some content about building add-ins for Office applications - both VSTO (managed code) add-ins and non-VSTO COM add-ins discusses these approaches. See:
- Andrew Whitechapel : Exposing Events From Non-VSTO Add-in Automation ...
- Microsoft Visual Studio Tools for the Microsoft Office System (scroll down to the link to Video Demo: VSTO and VBA Interop)
- Microsoft Visual Studio Tools for the Microsoft Office System (See other topics that may apply)
- VSTO: Build Office-Based Solutions Using WPF, WCF, And LINQ
http://msdn.microsoft.com/en-us/magazine/cc163292.aspx
256624 HOWTO: Use a COM Add-In Function as an Excel Worksheet Function
http://support.microsoft.com/default.aspx?scid=kb;EN-US;256624
A video about an documentation-level customization VBA interop with VSTO managed code in VS 2008 http://channel9.msdn.com/posts/DanielMoth/VBA-interop-with-VSTO-managed-code-in-VS-2008/
A link to MSDN content about a document-level customization is here:
http://msdn.microsoft.com/en-us/library/bb386306.aspx
Links to application-level customization are here:
Calling code in Application-Level Add-ins from other solutions.
http://msdn.microsoft.com/en-us/library/bb608621.aspx
Walkthrough: Calling Code in an Application-Level Add-in from VBA http://msdn.microsoft.com/en-us/library/bb608614.aspx
VSTO Add-ins, COMAddins and RequestCOMAddInAutomationService http://blogs.msdn.com/andreww/archive/2007/01/15/vsto-add-ins-comaddins-and-requestcomaddinautomationservice.aspx
COMAddIns Race Condition
http://blogs.msdn.com/andreww/archive/2008/08/13/comaddins-race-condition.aspx
Why your COMMAddIn.Object should derive from StandardOleMarshalObject http://blogs.msdn.com/andreww/archive/2008/08/11/why-your-comaddin-object-should-derive-from-standardolemarshalobject.aspx
Exposing Events from Managed Add-in Objects http://blogs.msdn.com/andreww/archive/2008/10/13/exposing-events-from-managed-add-in-objects.aspx
Hope this helps !Thanks,
Priyank- Marked as answer by priyankh11Moderator Friday, May 29, 2009 1:45 PM
Wednesday, May 20, 2009 1:36 AMModerator
All replies
-
Hi,
Thanks for contacting "Innovate on Office" forum. We have started research on it and will get back to you as soon as possible.
Regards,
Priyank
Friday, May 8, 2009 11:33 PMModerator -
Hi,
The button is not a System.Windows.Form.Button. It has no methods or events that are part of a Form Button. An OLEObject is either a linked or an embedded object that has an action verb. The two action verbs for most OLE Objects are "Edit" and "Open". The vButton created by your code has neither of those verbs, or any other verb.Instead of using the "Forms.CommandButton.1" object please consider using the custom button shape with code that looks like the following:
PowerPoint.Shape vButton = Sld.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeActionButtonCustom, 30, 480, 105, 30);
Then give the shape (Custom Button) recognition of the mouse clike with code that looks like:
vButton.ActionSettings[Microsoft.Office.Interop.PowerPoint.PpMouseActivation.ppMouseClick].Action = PowerPoint.PpActionType.ppRunMacro;
followed by
vButton.ActionSettings[PowerPoint.PpMouseActivation.ppMouseClick].Run = "Test";
where the macro named 'Test' is a PowerPoint VBA macro that initiates a VBA Form. You build the macro into an existing presentation. Also, in the VBA IDE you add a form from the "Tools" menu dropdown. A simple macro named "Test" to call that form is
Sub Test()
UserForm1.Show
End Sub
Alternatively, you can present a Windows Form by running an Windows Forms executable. The code to do that using the Custom Button shape to call your executable named "C:\MyPrograms\MyForm.exe" is:
vButton.TextFrame.TextRange.Text = "Run My Form"; vButton.ActionSettings[PowerPoint.PpMouseActivation.ppMouseClick].Action = PowerPoint.PpActionType.ppActionRunProgram;
vButton.ActionSettings[PowerPoint.PpMouseActivation.ppMouseClick].Run = @"C:\MyPrograms\MyForm.EXE"; // These two lines are a repetition of the two above - they're necessary // because of an idiosyncrasy in PowerPoint 2007 vButton.ActionSettings[PowerPoint.PpMouseActivation.ppMouseClick].Action = PowerPoint.PpActionType.ppActionRunProgram;
vButton.ActionSettings[PowerPoint.PpMouseActivation.ppMouseClick].Run = @"C:\MyPrograms\MyForm.EXE";
Another alternative might be to use a webpage. The following code demonstrates how to call a webpage:
vButton.TextFrame.TextRange.Text = "Show MSN"; vButton.ActionSettings[PowerPoint.PpMouseActivation.ppMouseClick].Action = PowerPoint.PpActionType.ppActionHyperlink;
vButton.ActionSettings[PowerPoint.PpMouseActivation.ppMouseClick].Hyperlink.Address = "http:\\www.msn.com";
Hope this helps!
Thanks,
PriyankTuesday, May 12, 2009 1:19 PMModerator -
Hi,
Thank you very much for your kind reply.
I am creating an PowerPoint Add-in, which adds an Shape to the PowerPoint Slide, when I click that Shape it should show the windows form (which is created in the project).
Kind Regards,
brWednesday, May 13, 2009 9:45 AM -
Hi,So that we can better understand how to answer your request please send us a zip of the add-in project that generates the form. Once we understand what you want to show and how it gets into the add-in's program flow we can better assess how to get the shape to display the form.
You can send zipped code to the following email address as priyankh@hotmail.com. Please give the subject as "Innovate on Office -Click event handler for command button (on the powerpoint slide) "
Thanks,
Priyank
Wednesday, May 13, 2009 9:32 PMModerator -
Hi,
Thanks for your reply. I have sent the email with the code. Please help me.
Regards,
brTuesday, May 19, 2009 1:01 PM -
Hi,
We have reviewed your code and following is our assesment,
The third shape, and most significant shape added to the slide, is a Microsoft Office VBA Forms2 button named "vButton" which is placed on the slide using the following 2 lines of code:PowerPoint.Shape vButton = Sld.Shapes.AddOLEObject(10, 500, 100, 30, "Forms.CommandButton.1", null, Office.MsoTriState.msoFalse, null, 0, null, Office.MsoTriState.msoFalse);
vButton.Name = "btnVoting";
The C# code successfully places the button on the slide, where it shows click behavior, but does nothing. That is because the button fires only one event - Click. The sink, or listener for that event is in a Forms 2.0 form. Expressed another way, the only object that hears the click is a Forms 2.0 form. There is no way for the PowerPoint add-in to respond to the click of that button.
In previous logs in this thread there have been examples for using the "ActionButton: Custom Action Button" shape and setting its acction to msoActionRunMacro. If the PowerPoint presentation is designed in advance you can add a VBA macro to it that calls a Form built by the Forms 2.0 dll.
You build the form on the editing surface of the VBA IDE. To see this surface you need to have a "Developer" tab in the ribbon at the top of the PowerPoint design screen. To get that, if you don't already have one, click the round "Office" button at the left end of the ribbon. In the dropdown page, at the bottom there is a "PowerPoint Options" button. Click that. The PowerPoint Options dialog opens, with the default page "Popular" showing. Click to check the box beside the line that says "Show Developer tab in the Ribbon", then click the OK button at the bottom to get back to the presentation design screen.
The "Developer" tab will be at the right end of the ribbon. Click it to open the Developer ribbon. At the left end there is a button named "Macros" Click it. In the dialog that opens create a macro named "Test" and click OK. The VBA IDE will open,and show the empty macro that looks like this:
Sub Test()
End Sub
We'll come back to that in a minute, but first you want to design a form. In the VBA IDE menu bar click the "Insert" menu, and select "User Form". The UserForm1 design window will open and a small Toolbox will float beside it. Use those tools to build the form. Also, notice at the left of the screen there is a Property box for the form. You can change properties to tailor the appearance of the form. When you have the form, minimize it and go back to the macro.
Assuming the form is named "UserForm1" the macro can be as simple as Sub Test()
UserForm1.Show
End Sub
Back on the slide the Custom action button's action is to run the macro which calls the form.
The line in the C# code that says oShape.ActionSettings[PowerPoint.PpMouseActivation.ppMouseClick].Run = "Test"; specifically runs that macro.
Alternatively,from an object on the presentation you can call into an add-in to execute a member method. There are several approaches. Some content about building add-ins for Office applications - both VSTO (managed code) add-ins and non-VSTO COM add-ins discusses these approaches. See:
- Andrew Whitechapel : Exposing Events From Non-VSTO Add-in Automation ...
- Microsoft Visual Studio Tools for the Microsoft Office System (scroll down to the link to Video Demo: VSTO and VBA Interop)
- Microsoft Visual Studio Tools for the Microsoft Office System (See other topics that may apply)
- VSTO: Build Office-Based Solutions Using WPF, WCF, And LINQ
http://msdn.microsoft.com/en-us/magazine/cc163292.aspx
256624 HOWTO: Use a COM Add-In Function as an Excel Worksheet Function
http://support.microsoft.com/default.aspx?scid=kb;EN-US;256624
A video about an documentation-level customization VBA interop with VSTO managed code in VS 2008 http://channel9.msdn.com/posts/DanielMoth/VBA-interop-with-VSTO-managed-code-in-VS-2008/
A link to MSDN content about a document-level customization is here:
http://msdn.microsoft.com/en-us/library/bb386306.aspx
Links to application-level customization are here:
Calling code in Application-Level Add-ins from other solutions.
http://msdn.microsoft.com/en-us/library/bb608621.aspx
Walkthrough: Calling Code in an Application-Level Add-in from VBA http://msdn.microsoft.com/en-us/library/bb608614.aspx
VSTO Add-ins, COMAddins and RequestCOMAddInAutomationService http://blogs.msdn.com/andreww/archive/2007/01/15/vsto-add-ins-comaddins-and-requestcomaddinautomationservice.aspx
COMAddIns Race Condition
http://blogs.msdn.com/andreww/archive/2008/08/13/comaddins-race-condition.aspx
Why your COMMAddIn.Object should derive from StandardOleMarshalObject http://blogs.msdn.com/andreww/archive/2008/08/11/why-your-comaddin-object-should-derive-from-standardolemarshalobject.aspx
Exposing Events from Managed Add-in Objects http://blogs.msdn.com/andreww/archive/2008/10/13/exposing-events-from-managed-add-in-objects.aspx
Hope this helps !Thanks,
Priyank- Marked as answer by priyankh11Moderator Friday, May 29, 2009 1:45 PM
Wednesday, May 20, 2009 1:36 AMModerator -
Hi,
I visited http://blogs.msdn.com/andreww/archive/2009/03/01/exposing-events-from-non-vsto-add-in-automation-objects.aspx . I need some more explanation inline with my problem.
I have placed a button on all slides in my powerpoint presentation. Am developing a desktop application such that one can start/pause/resume/stop recording audio during a slideshow. How can I put an event such that the button can work with the desktop application?
I welcome your advice!Simon.
Wednesday, March 31, 2010 7:24 PM