locked
powerpoint Slide and c# RRS feed

  • Question

  • Hello,

    How can I add a command button to a PowerPoint Slide? Please help me.

    Regards,
    br
    Wednesday, April 22, 2009 9:54 AM

Answers

All replies

  • Hi,

    Thank you for contacting Microsoft Innovate on office Forum!

    Could you please give me below details to provide you the service ASAP:

    1.    What is the version of powerpoint that you are using?

    2.    Do you want to add this command button programmatically?

    Hope to hear back from you soon!

     

    Thanks,
    Dhanashri.

    Wednesday, April 22, 2009 11:19 PM
    Moderator
  • Hi,

    Thanks for your reply.

    Here are my answers,
     
    1. PowerPoint 2007, Visual Studio 2008, and C# as programming language,
    2. Yes I want to add command button programmatically.

    I used PowerPoint.Shape pt = Sld.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeActionButtonCustom, 100, 100, 100, 100); to add the button on the slide. Now how can I add events or actions to that button.

    Please help me.

    Kind Regards,
    BR
    Thursday, April 23, 2009 8:21 AM
  • using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Linq;

    using System.Text;

    using System.Windows.Forms;

    using Office = Microsoft.Office.Core;

    using PowerPoint = Microsoft.Office.Interop.PowerPoint;

     

    namespace PowerPoint2007Events

    {

        public partial class Form1 : Form

        {

            public Form1()

            {

                InitializeComponent();

            }

     

            private void button1_Click(object sender, System.EventArgs e)

            {

                // Create an instance of PowerPoint, then load an existing presentation.

                PowerPoint._Application oPPT = new PowerPoint.Application();

                oPPT.Visible = Office.MsoTriState.msoTrue;

                PowerPoint.Presentations oPresentations = oPPT.Presentations;

                PowerPoint.Presentation oPresentation = oPresentations.Open("C:\\Presentations\\Show1.pptx", Office.MsoTriState.msoFalse, Office.MsoTriState.msoTrue, Office.MsoTriState.msoTrue);

                int iCount = 4;

                PowerPoint.Slide oSlide = oPresentation.Slides[4];

                PowerPoint.Shapes oShapes = oSlide.Shapes;

     

                PowerPoint.Shape oShape = oSlide.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeActionButtonCustom, 30, 480, 54, 30);

     

                try

                {              

    oShape.ActionSettings[Microsoft.Office.Interop.PowerPoint.

                }

    PpMouseActivation.ppMouseClick].Action = PowerPoint.PpActionType.ppActionLastSlide;

     

     

     

                catch (Exception ex)

                {

                    Console.WriteLine("{0} Exception caught.", ex);

                }

                oShape.ActionSettings[PowerPoint.PpMouseActivation.ppMouseClick].Run = "Test";

            }

     

            private void Form1_Load(object sender, EventArgs e)

            {

     

            }

        }

    }

     

     

    Some content references which may be helpful to you:

     

    303718          How to use Automation to create and to show a PowerPoint 2002 presentation by using Visual C# .NET 2002

    http://support.microsoft.com/default.aspx?scid=kb;EN-US;303718

     

    303296          How To Use Automation to Get and to Set Office Document Properties with Visual C# .NET

    http://support.microsoft.com/default.aspx?scid=kb;EN-US;303296

     

    316126    How to use Visual C# to automate a running instance of an Office program

    http://support.microsoft.com/default.aspx?scid=kb;EN-US;316126

     

    308825          How to handle PowerPoint events with Visual C# .NET

    http://support.microsoft.com/default.aspx?scid=kb;EN-US;308825

     

    823981                HOW TO: Handle Events for Excel by Using Visual C# .NET

    http://support.microsoft.com/default.aspx?scid=kb;EN-US;823981

     

    824022               How to handle events in Word by using Visual C# .NET

    http://support.microsoft.com/default.aspx?scid=kb;EN-US;824022

     

    Please let me know whether this information is helpful or not.

     

    Thanks,

    Dhanashri.

     

    Disclaimer:

     

    By using the following materials or sample code you agree to be bound by the license terms below and the Microsoft Partner Program Agreement the terms of which are incorporated herein by this reference. These license terms are an agreement between Microsoft Corporation (or, if applicable based on where you are located, one of its affiliates) and you. Any materials (other than the sample code) we provide to you are for your internal use only. Any sample code is provided for the purpose of illustration only and is not intended to be used in a production environment. We grant you a nonexclusive, royalty-free right to use and modify the sample code and to reproduce and distribute the object code from the sample code, provided that you agree: (i) to not user Microsoft’s name, logo, or trademarks to market your software product in which the sample code is embedded; (ii) to include a valid copyright notice on your software product in which the sample code is embedded; (iii) to provide on behalf of and for the benefit of your subcontractors a disclaimer of warranties, exclusion of liability for indirect and  consequential damages and a reasonable limitation of liability; and (iv) to indemnify, hold harmless, and defend Microsoft, its affiliates and suppliers from and against any third party claims or lawsuits, including attorneys’ fees, that arise or result from the use or distribution of the sample code.

     

     

    Thursday, May 7, 2009 11:47 PM
    Moderator
  • Hi Dhanashri,

    Thanks for your reply.

    I have added a shape to the PowerPoint Slide. Now I have another question,

    -> when I click the button, a form should start. How can I do that?

    Please help me.

    Kind Regard,
    br


    Friday, May 8, 2009 7:08 AM
  • Are you talking about the windows form? if not, please explain.

    Friday, May 8, 2009 8:26 PM
    Moderator
  • Hi,

    Thanks for the reply.

    Yes, I am talking about the windows form.

    Regards,
    br
    Monday, May 11, 2009 12:27 PM
  • There are several ways for a custom button shape to do what you want:

     

    1.    With a macro. The button can call a VBA macro that is part of the presentation. The macro (let's call it "Test”) can then call an external application which produces the form, or can call an in-process form from the PowerPoint VBA Tools menu. A PowerPoint presentation with macros must be saved with a .pptm file extension.

     

    To call a macro from the button changes the line in the code from:

    "oShape.ActionSettings[Microsoft.Office.Interop.PowerPoint.PpMouseActivation.ppMouseClick].Action = PowerPoint.PpActionType.ppActionLastSlide; "

    To:

    "oShape.ActionSettings[Microsoft.Office.Interop.PowerPoint.PpMouseActivation.ppMouseClick].Action = PowerPoint.PpActionType.ppRunMacro; "

     

    The actual macro could be as simple as:

    Sub Test()

        UserForm1.Show

        UserForm1.BackColor = RGB(0, 255, 0) End Sub

     

    Then the line in the sample code that says oShape.ActionSettings[PowerPoint.PpMouseActivation.ppMouseClick].Run = "Test"; specifically runs that macro.

     

    2.    If you want to use a custom Winform that is generated by a program, let's call his program "MyForm.exe" the code can be changed to "oShape.ActionSettings[Microsoft.Office.Interop.PowerPoint.PpMouseActivation.ppMouseClick].Action = PowerPoint.PpActionType.ppRunProgram; "

     

    Change the next line in the program so that it now says oShape.ActionSettings[PowerPoint.PpMouseActivation.ppMouseClick].Run = @"c:\MyPrograms\MyForm.exe";

     

    Because PowerPoint 2007 has an idiosyncrasy that means it will not run a program unless you tell it to do it twice the actual code that will work is oShape.ActionSettings[PowerPoint.PpMouseActivation.ppMouseClick].Action = PowerPoint.PpActionType.ppActionRunProgram;

    oShape.ActionSettings[PowerPoint.PpMouseActivation.ppMouseClick].Run = @"C:\MyPrograms\MyForm.exe ";

                    // These next two lines are a repetition of the two above - they're necessary

                    // because of an idiosyncrasy in PowerPoint 2007 oShape.ActionSettings[PowerPoint.PpMouseActivation.ppMouseClick].Action = PowerPoint.PpActionType.ppActionRunProgram;

    oShape.ActionSettings[PowerPoint.PpMouseActivation.ppMouseClick].Run = @"C:\MyPrograms\MyForm.exe";

     

    3.    Using Hyperlink:

     

    Another method of making the Custom Button shape display something in addition to the slide in the presentation is to use a hyperlink.

     Sample code is

     

     oShape.ActionSettings[PowerPoint.PpMouseActivation.ppMouseClick].Action = PowerPoint.PpActionType.ppActionHyperlink;

    oShape.ActionSettings[PowerPoint.PpMouseActivation.ppMouseClick].Hyperlink.Address =  “http:\\www.contosso.com”

     

    Disclaimer

    By using the following materials or sample code you agree to be bound by the license terms below and the Microsoft Partner Program Agreement the terms of which are incorporated herein by this reference. These license terms are an agreement between Microsoft Corporation (or, if applicable based on where you are located, one of its affiliates) and you. Any materials (other than the sample code) we provide to you are for your internal use only. Any sample code is provided for the purpose of illustration only and is not intended to be used in a production environment. We grant you a nonexclusive, royalty-free right to use and modify the sample code and to reproduce and distribute the object code from the sample code, provided that you agree: (i) to not user Microsofts name, logo, or trademarks to market your software product in which the sample code is embedded; (ii) to include a valid copyright notice on your software product in which the sample code is embedded; (iii) to provide on behalf of and for the benefit of your subcontractors a disclaimer of warranties, exclusion of liability for indirect and consequential damages and a reasonable limitation of liability; and (iv) to indemnify, hold harmless, and defend Microsoft, its affiliates and suppliers from and against any third party claims or lawsuits, including attorneys fees, that arise or result from the use or distribution of the sample code.

    Tuesday, May 12, 2009 1:08 AM
    Moderator
  • Hi,

    Thanks for your reply.

    oShape.ActionSettings[PowerPoint.PpMouseActivation.ppMouseClick].Action = PowerPoint.PpActionType.ppActionRunProgram;

    oShape.ActionSettings[PowerPoint.PpMouseActivation.ppMouseClick].Run = @"C:\MyPrograms\MyForm.exe";

     

    has worked well, but here we are writing an another program "MyForm.exe". When a write an add-in for powerpoint (which creates a button on slide, by clicking that button windows form is shown), how can I link the button with the form directly (in a project)? Is there any way to do that a part from this way

    oShape.ActionSettings[PowerPoint.PpMouseActivation.ppMouseClick].Run = @"C:\MyPrograms\MyForm.exe"; ?

     

    Kind Regards,

    BR


     


    Tuesday, May 12, 2009 6:46 AM
  • Hi,

    As I understand by your last post, you want to activate a "Form" from a button click.  Could you please explain what kind of form you want to activate?

     

    Please find some of the below observation in this regard:

    a.    If you are considering Window’s Form:

     

    1.    A Windows’s Form is hosted in an .exe. 

    2.    A Window’s Forms Control is a .dll (dynamic link library) which can be hosted in an Internet Explorer web page, but not hosted in the PowerPoint application.

    3.    You can build an ActiveX control that has the appearance of a form and, if desired has data binding. You can embed it on a slide. This control becomes part of the slide, not a free-floating form.

     

    a.    If you are talking about the InfoPath Form:

     

    There is no way to call a Microsoft InfoPath form from a button on a slide. The button can call an .exe which runs InfoPath and opens a specific Form from a SharePoint Forms server.

     

    From the list above, only the first option appears to be workable.

    Please let us know if you think the above options answers your question(s).

     

     

    Thanks,

    Dhanashri.

    Monday, May 18, 2009 8:17 PM
    Moderator
  • Hi,

    Thanks for your reply.

    Windows form is created within the same project. For better understanding I will send you the code, please give me your email id.

    kind regards,
    br
    Tuesday, May 19, 2009 1:05 PM
  • Hi,

    We see that you have posted the same question at http://social.msdn.microsoft.com/Forums/en-US/innovateonoffice/thread/029a2cc7-4f3c-467c-95db-c4102d4a99f8 and one of our consultant is already working with you.
    So we are closing this thread as a duplicate and for further updates please visit:

    Click event handler for command button (on the powerpoint slide)
    http://social.msdn.microsoft.com/Forums/en-US/innovateonoffice/thread/029a2cc7-4f3c-467c-95db-c4102d4a99f8


    Thanks,
    Dhanashri.
    Tuesday, May 19, 2009 10:03 PM
    Moderator