This article provides the minimum steps needed to create an
automation controller to manipulate the Microsoft PowerPoint object model using
the Microsoft Foundation Classes (MFC).
This article is designed as a
tutorial. The tutorial makes the following assumptions:
You are familiar with Visual C++.
You are familiar with how MFC applications are
written.
You have Microsoft PowerPoint installed on your development
machine.
You have Microsoft Visual C++ installed on your development
machine.
The sample application you create uses menu options to control
the behavior of PowerPoint. The tutorial demonstrates How To
Use the PowerPoint 97 object library (msppt8.olb), the
PowerPoint 2000 object library (msppt9.olb) or the PowerPoint 2002 object
library (msppt.olb) to create COleDispatchDriver wrapper classes.
Connect to the application object.
Create presentations.
Create slides.
Create shapes on a slide.
Run a slide show.
NOTE: This article is designed to show you the basics of controlling
the PowerPoint object model with a Visual C++ MFC application. The code created
is not intended to be production quality. The coding techniques were selected
to simplify the process as much as possible.
NOTE: As an alternative to using the Microsoft Foundation Classes, you
can use the #import feature of the Visual C++ compiler to convert the contents
of a type library into Visual C++ classes.
For more information on
the #import directive, please refer to the online documentation. For a code
sample, please see the COMEXCEL sample.
Microsoft provides examples of Visual Basic for
Applications and Visual C++ procedures for illustration only, without warranty
either expressed or implied, including, but not limited to the implied
warranties of merchantability and/or fitness for a particular purpose. The
Visual C++ procedures in this article are provided "as is" and Microsoft does
not guarantee that they can be used in all situations. While Microsoft support
engineers can help explain the functionality of a particular macro, they will
not modify these examples to provide added functionality, nor will they help
you construct macros to meet your specific needs. If you have limited
programming experience, you may want to consult one of the Microsoft Solution
Providers. Solution Providers offer a wide range of fee- based services,
including creating custom macros. For more information about Microsoft Solution
Providers, call Microsoft Customer Information Service at (800) 426- 9400.
Step 1: Create the Application Framework with the AppWizard
Start Microsoft Visual C++ version 5.0.
Create a new MFC AppWizard project. To do this:
On the File menu, click New.
If not selected, click the Projects tab.
Click the MFC AppWizard (exe) project type.
In the Project name field, type a name for your
project. These steps assume you use the name TestPPT.
Click OK to start the MFC AppWizard.
Create an SDI application with OLE Automation support. To
do this:
Click Single document and click Next.
Click Next again. This sample doesn't need database
support.
Deselect the ActiveX Controls check box and select the
Automation check box. Click Finish (you don't need to customize any of the
other steps in the AppWizard for this sample.)
Click OK to accept the skeleton project
specifications.
Step 2: Create COleDispatchDriver Wrapper Classes Based on Msppt8.olb (PowerPoint 97), Msppt9.olb (PowerPoint 2000), or Msppt.olb (PowerPoint 2002)
On the View menu, click ClassWizard.
Click the Add Class button and click From a Type Library on
the drop- down list.
In the Import from Type Library dialog box, navigate to the
location of the Msppt8.olb file for Powerpoint 97, Msppt9.olb file for
PowerPoint 2000, or Msppt.olb for PowerPoint 2002 (the .olb file is found in
the Office folder). If you selected the default installation options for
Microsoft Office, the path should look like this:
C:\Program Files\Microsoft Office\Office
Click Msppt8.olb/Msppt9.olb/Msppt.olb, and then click
Open.
Select the classes you want to use in your
application.
NOTE: At any time you may add additional classes from the
Msppt8.olb/Msppt9.olb/Msppt.olb file, so the choices you make now are not
permanent.
For this tutorial select the following classes (to select
multiple classes, hold down the Control key and select the classes you want):
Create a PowerPoint menu for the MFC application with the
following options:
Start PowerPoint
Create Presentation
Create Slide
Add Shape
Start SlideShow
Quit PowerPoint
To do this:
Click the ResourceView tab in your workspace.
Open the TestPPT resources folder.
Open the Menu folder.
Double-click the IDR_MAINFRAME resource.
Double-click the rectangle, which has no text, to the right
of the Help menu. This opens the Menu Item Properties dialog box.
Create the PowerPoint menu by clicking the General tab and
typing the following text in the Caption field:
PowerPoint
Create the Start PowerPoint menu option by double-clicking
the rectangle underneath PowerPoint and typing the following text into the
Caption field:Start PowerPoint
Create the Create Presentation menu option by
double-clicking the rectangle underneath Start PowerPoint and typing the
following text into the Caption field: Create
Presentation
Create the Create Slide menu option by double-clicking the
rectangle underneath Create Presentation and typing the following text into the
Caption field: Create Slide
Create the Add Shape menu option by double-clicking the
rectangle underneath Create Slide and typing the following text into the
Caption field: Add Shape
Create the Slide Show menu option and its sub menus by
double-clicking the rectangle underneath Add Shape and typing the following
text into the Caption field: Start
SlideShow
Create the Quit PowerPoint menu option by double-clicking
the rectangle underneath Slide Show and typing the following text into the
Caption field: Quit PowerPoint
In ResourceView, right-click the menu option Start
PowerPoint and click ClassWizard.
Select the COMMAND message, and click Add Function. This
opens the Add Member Function dialog box.NOTE: Make sure the Project name is TestPPT and the Class name is
CMainFrame.
Provided you didn't change the name of the resource ID, the
name of your handler should be:
OnPowerpointStartpowerpoint
Click OK to accept this name.
Repeat steps 1 through 3 to create handlers for the
remaining menu options. After all handlers are created, click OK to close the
ClassWizard.
Modify the CMainFrame class constructor to connect to the
application object. When you are finished, the CMainFrame constructor should
look like this:
CMainFrame::CMainFrame()
{
// Create an instance of the PowerPoint application.
m_ppt.CreateDispatch("PowerPoint.Application");
}
Add the following code for the command handlers:Start PowerPoint:
void CMainFrame::OnPowerpointStartpowerpoint()
{
// Check if the IDispatch connection exists with PowerPoint,
// if not create one.
if (m_ppt.m_lpDispatch == NULL) {
// Create IDispatch connection to PowerPoint.
m_ppt.CreateDispatch("PowerPoint.Application");
};
// Bring the PowerPoint application to the front.
m_ppt.Activate();
}
Start SlideShow:
void CMainFrame::OnPowerpointStartslideshow()
{
_Presentation oPresentation;
SlideShowSettings oShow;
// Attach to the Active Presentation.
oPresentation.AttachDispatch(m_ppt.GetActivePresentation());
// Attach to the slide-show settings.
oShow.AttachDispatch(oPresentation.GetSlideShowSettings());
// Run the slide show.
oShow.Run();
}
Quit PowerPoint:
void CMainFrame::OnPowerpointQuitpowerpoint()
{
// Check if PowerPoint is still running. If
// PowerPoint is not running, quit PowerPoint
// and release the dispatch pointer.
if(m_ppt.m_lpDispatch != NULL) {
// Quit PowerPoint. Note, the Quit command exits
// PowerPoint without displaying any dialog boxes. So,
// any unsaved data is lost.
m_ppt.Quit();
// Free the dispatch. This sets m_lpDispatch to NULL.
m_ppt.ReleaseDispatch();
};
}
Create Slide:
void CMainFrame::OnPowerpointCreateslide()
{
// Connect to the active presentation. There is no error trapping.
// If the active presentation the framework traps
// the error and displays a message box.
_Presentation ActivePresentation(m_ppt.GetActivePresentation());
// Connect to the slides collection.
Slides oSlides(ActivePresentation.GetSlides());
// This constant is defined in the PowerPoint Object model.
// You can use the Object Browser, with Visual Basic Editor
// (VBE), to look up the different constant values.
const ppLayoutTitleOnly = 11;
// Add a new slide to the presentation. This code adds the new
// slide to the end of the presentation.
oSlides.Add(oSlides.GetCount() + 1l, ppLayoutTitleOnly);
}
Create Presentation:
void CMainFrame::OnPowerpointCreatepresentation()
{
Presentations PresCollection;
// Make sure there is a dispatch pointer for PowerPoint.
if(m_ppt.m_lpDispatch == NULL) {
// Display a message indicating that PowerPoint is not running.
MessageBox("PowerPoint is not running.", "Start PowerPoint");
} else {
// Bring PowerPoint to the front.
m_ppt.Activate();
// Attach the presentations collection to the PresCollection
// variable.
PresCollection.AttachDispatch(m_ppt.GetPresentations());
// Create a new presentation.
PresCollection.Add(1);
};
}
Add Shape:
void CMainFrame::OnPowerpointAddshape()
{
// Connect to the active presentation object.
_Presentation ActivePresentation(m_ppt.GetActivePresentation());
// Connect to the Slides collection object.
Slides oSlides(ActivePresentation.GetSlides());
// Connect to the first slide in the presentation.
long lIndex = 1;
COleVariant SlideNumber(lIndex);
_Slide oSlide(oSlides.Item(SlideNumber));
// Connect to the Shapes collection.
Shapes oShape(oSlide.GetShapes());
// Create the heart shape on the slide.
const long msoShapeHeart = 21;
float l = 50,t = 150,w = 350,h = 350;
oShape.AddShape(msoShapeHeart, l, t, w, h);
}
NOTE: The first time you build the project it takes longer than normal
to build. This is because the compiler creates a pre-compiled header (.pch) for
the project. The .pch file, for your project, is roughly four megabytes in
size.
On the Build menu, click Execute TestPPT.exe. Try selecting
the different options on the PowerPoint menu.