HOWTO: Use Automation to Create & Show a PowerPoint Presentation

Last reviewed: February 9, 1998
Article ID: Q180616
The information in this article applies to:
  • Microsoft Visual C++, 32-bit Editions, version 5.0
  • Microsoft PowerPoint 97 for Windows
  • The Microsoft Foundation Classes (MFC)

SUMMARY

This article shows how to automate Microsoft PowerPoint 97 using the Microsoft Foundation Class (MFC) library, version 4.2 (installed with Microsoft Visual C++ version 5.0). Specifically, this article illustrates how to create and show a PowerPoint presentation with automation.

MORE INFORMATION

You can copy the code in this article to the message handler function of an event defined in an MFC .cpp file. However, the purpose of the code is to illustrate the process of using the IDispatch interfaces and member functions defined in the Msppt8.olb type library. The primary benefit comes from reading and understanding the code in the example so that you can modify the example or write your own code to automate Microsoft PowerPoint 97 using MFC.

Steps to Create the Project

  1. Follow steps 1 through 12 in the following Microsoft Knowledge Base article to create a sample project that uses the IDispatch interfaces and member functions defined in the Msppt8.olb type library:

          ARTICLE-ID: Q178749
    
          TITLE     : HOWTO: Create an Automation Project Using MFC and a Type
                      Library
    
    

  2. Press CTRL+W to display the ClassWizard once more and select the Automation tab. Click the Add Class button and select From a Type Library. Locate Graph8.olb and click Open. Select only the "Chart" class and click OK. Click OK to close the ClassWizard.

    NOTE: The presentation that you will create with this example contains an embedded Microsoft Graph chart; this is the reason for adding the Chart class from the Microsoft Graph type library (Graph8.olb).

  3. At the top of the AutoProjectDlg.cpp file, add the following lines:

          #include "msppt8.h"
          #include "graph8.h"
    

  4. Add the following code to CAutoProjectDlg::OnRun() in the AutoProjectDlg.cpp file.

          // Commonly used OLE-variants.
          COleVariant
    
                      covTrue((short)TRUE),
                      covFalse((short)FALSE),
                      covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
    
          _Application   app;
          Presentations  presentations;
          _Presentation  presentation;
          Slides         slides;
          _Slide         slide;
          ShapeRange     shaperange;
          Shapes         shapes;
          Shape          shape;
          TextFrame      textframe;
          TextRange      textrange;
          Font           font;
          FillFormat     fillformat;
          ColorFormat    colorformat;
          ShadowFormat   shadow;
    
          CString strOfficePath =
             "C:\\Program Files\\Microsoft Office";
    
          if(!app.CreateDispatch("Powerpoint.Application"))
          {
             AfxMessageBox("Could not create Powerpoint object.");
             return;
          }
    
          //Make the application visible and minimized.
          app.SetVisible((long)TRUE);
          app.SetWindowState((long) 2);   //ppWindowMinimized=2
    
          //Get the Presentations collection and create a new presentation
          //from a template(.POT)
          presentations.AttachDispatch(app.GetPresentations());
          CString strTemplate;
          strTemplate = strOfficePath +
             CString("\\Templates\\Presentation Designs\\Dads Tie.pot");
          presentation = presentations.Open(
             strTemplate,   //File name
             (long)0,       //Read-only
             (long)-1,      //Untitled
             (long)-1       //WithWindow
             );
    
          slides = presentation.GetSlides();
    
          //Add the first slide to the presentation.
          slide = (slides.Add((long)1, (long)11));   //pptLayoutTitleOnly=11
    
          //Modify the text and font name of the TextFrame on the slide.
          shapes = slide.GetShapes();
          shape = shapes.Item(COleVariant((short)1));
          textframe = shape.GetTextFrame();
          textrange = textframe.GetTextRange();
          textrange.SetText("My Sample Presentation");//Set the text
          font = textrange.GetFont();
          font.SetName("Comic Sans MS");              //Set the font name.
          font.SetSize((float)48);                    //Set the font size.
    
          //Insert a picture into the slide and position it.
          CString strPic1 =
             strOfficePath + CString("\\Clipart\\Popular\\Amhappy.wmf");
          shapes.AddPicture(
                       strPic1,      //Filename
                       (long)0,      //LinkToFile
                       (long)-1,      //SaveWithDocument
                       (float)150,   //Left
                       (float)150,   //Top
                       (float)500,   //Width
                       (float)350      //Height
                       );
    
          //Add the second slide to the presentation.
          slide = (slides.Add((long)2, (long)11));   //pptLayoutTitleOnly=11
    
          //Modify the text and font name of the
          //TextFrame on the slide.
          shapes = slide.GetShapes();
          shape = shapes.Item(COleVariant((short)1));
          textframe = shape.GetTextFrame();
          textrange = textframe.GetTextRange();
          textrange.SetText("Look! It's a Graph!");//Set the text
          font = textrange.GetFont();
          font.SetName("Comic Sans MS");           //Set the font name.
          font.SetSize((float)48);                 //Set the font size.
    
          //Add a Graph8 object to the slide.
          shape = shapes.AddOLEObject(
                         (float)150,      //Left
                         (float)150,      //Top
                         (float)480,      //Width
                         (float)320,      //Height
                         "MSGraph.Chart", //Classname
                         "",              //FileName
                         (long)0,         //DisplayAsIcon
                         "",              //IconFileName
                         (long)0,         //IconIndex
                         "",              //IconLabel
                         (long)0          //Link
                         );
    
          //Modify the charttype of the graph.
          Chart chart;
          OLEFormat olefmt = shape.GetOLEFormat();
          chart = olefmt.GetObject();
          chart.SetChartType((long)70);
    
          //Add the third slide to the presentation.
          slide = (slides.Add((long)3, (long)12));   //ppLayoutBlank = 12
    
          //Change the background color of the slide.
          slide.SetFollowMasterBackground((long)0);
          shaperange = slide.GetBackground();
          fillformat = shaperange.GetFill();
          colorformat = fillformat.GetForeColor();
          colorformat.SetSchemeColor((long)3);   //ppShadow = 3
    
          //Add Text Effects to the last slide
          shapes = slide.GetShapes();
          shape = shapes.AddTextEffect(
                         (long)27,    //PresetTextEffect (msoTextEffect28 = 27)
                         "The End",   //Text
                         "Impact",    //FontName
                         (float)96,   //FontSize
                         (long)0,     //FontBold
                         (long)0,     //FontItalic
                         (float)230,  //Left
                         (float)200   //Top
                         );
    
          //Apply a gradient fill to the texteffect.
          fillformat = shape.GetFill();
          colorformat = fillformat.GetForeColor();
          colorformat.SetSchemeColor((long)2);   //ppForeground = 2
          colorformat = fillformat.GetBackColor();
          colorformat.SetSchemeColor((long)4);   //ppTitle = 4
          fillformat.TwoColorGradient ((long)1, (long)1);
                                                 //msoGradientHorizontal = 1
    
          //Apply a shadow to the texteffect.
          shadow = shape.GetShadow();
          colorformat = shadow.GetForeColor();
          colorformat.SetSchemeColor((long)2);   //ppForeground = 2
          shadow.SetVisible((long)-1);
          shadow.IncrementOffsetX((float)3);
          shadow.IncrementOffsetY((float)3);
    
          SlideShowTransition   show;
          SlideShowSettings   slideshow;
    
          //Set the Transition effects for Slide 1.
          slide = slides.Item(COleVariant((short)1));
          show = slide.GetSlideShowTransition();
          show.SetEntryEffect((long)769);      //ppEffectBlindsHorizontal=769
          show.SetAdvanceOnTime((long)-1);
          show.SetAdvanceTime((float)3);
    
          //Set the Transition effects for Slide 2.
          slide = slides.Item(COleVariant((short)2));
          show = slide.GetSlideShowTransition();
          show.SetEntryEffect((long)770);      //ppEffectBlindsVertical=770
          show.SetAdvanceOnTime((long)-1);
          show.SetAdvanceTime((float)3);
    
          //Set the Transition effects for Slide 3.
          slide = slides.Item(COleVariant((short)3));
          show = slide.GetSlideShowTransition();
          show.SetEntryEffect((long)3073);      //ppEffectBoxOut=3073
          show.SetAdvanceOnTime((long)-1);
          show.SetAdvanceTime((float)3);
    
          //Run the show.
          slideshow = presentation.GetSlideShowSettings();
          slideshow.Run();
    
          //Quit the application after waiting 9 seconds (the length of the
          //slide show).
          ::Sleep(9000);
          presentation.SetSaved((long)-1);
          app.Quit();
    
    

  5. Modify the following line of code in CAutoProjectDlg::OnRun(), if necessary, to specify the correct path for your installation of Microsoft Office.

          CString strOfficePath =
    
             "C:\\Program Files\\Microsoft Office";
    


Additional query words: PPT PPT97
Keywords : MfcOLE kbinterop
Version : WINDOWS:97; WINNT:5.0
Platform : WINDOWS winnt
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 9, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.