The information in this article applies to:
- Microsoft PowerPoint 97 for Windows
- The Microsoft Foundation Classes (MFC) included with:
- Microsoft Visual C++, 32-bit Editions, version 5.0
SUMMARY
This article provides the minimum steps needed to create an automation
controller to manipulate the Microsoft PowerPoint 97 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 97 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 object library (msppt8.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.
MORE INFORMATION
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:
a. On the File menu, click New.
b. If not selected, click the Projects tab.
c. Click the MFC AppWizard (exe) project type.
e. In the Project name field, type a name for your project. These
steps assume you use the name TestPPT.
f. Click OK to start the MFC AppWizard.
- Create an SDI application with OLE Automation support. To do this:
a. Click Single document and click Next.
b. Click Next again. This sample doesn't need database support.
c. 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.)
d. Click OK to accept the skeleton project specifications.
Step 2: Create COleDispatchDriver Wrapper Classes Based on Msppt8.olb
- 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 (the .olb file is found in the Office folder).
If you selected the default installation options, for Microsoft Office
97, the path should look like this:
C:\Program Files\Microsoft Office\Office
- Click Msppt8.olb and 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
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):
- _Application
- SlideShowSettings
- Presentations
- Slides
- _Slide
- Shapes
- _Presentation
Click OK when you have finished selecting the classes above, and then
click OK to close the MFC ClassWizard.
Step 3: Create a PowerPoint Menu
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
- Close the Menu Item Properties dialog box.
Step 4: Create Hooks for the Menu Option Handlers
- 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.
Step 5: Add the Command Handler Code
- Follow these steps to create a member variable to hold the application
object:
a. Click the ClassView tab in your workspace.
b. Open TestPPT classes.
c. Right-click the CMainFrame class icon and click Add member variable
from the context menu.
d. Enter the following information into the Add Member Variable dialog
box:
Variable Type: _Application
Variable Declaration: m_ppt
Access: Private
Click OK when you are finished.
- 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.8");
}
- 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.8");
};
// 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);
}
Step 6: Build and Run the Application
- On the Build menu, click Build TestPPT.exe.
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.
|