How to create an MFC Automation client for PowerPoint


How to create an MFC Automation client for PowerPoint

















Article ID : 169505
Last Review : January 19, 2007
Revision : 3.6

This article was previously published under Q169505



SUMMARY


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.

Back to the top


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.

Back to the top


Step 1: Create the Application Framework with the AppWizard












1. Start Microsoft Visual C++ version 5.0.
2. 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.
d. In the Project name field, type a name for your project. These steps assume you use the name TestPPT.
e. Click OK to start the MFC AppWizard.
3. 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.

Back to the top


Step 2: Create COleDispatchDriver Wrapper Classes Based on Msppt8.olb (PowerPoint 97), Msppt9.olb (PowerPoint 2000), or Msppt.olb (PowerPoint 2002)


















1. On the View menu, click ClassWizard.
2. Click the Add Class button and click From a Type Library on the drop- down list.
3. 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
4. Click Msppt8.olb/Msppt9.olb/Msppt.olb, and then click Open.
5. 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):

    – _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.

Back to the top


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:








































1. Click the ResourceView tab in your workspace.
2. Open the TestPPT resources folder.
3. Open the Menu folder.
4. Double-click the IDR_MAINFRAME resource.
5. Double-click the rectangle, which has no text, to the right of the Help menu. This opens the Menu Item Properties dialog box.
6. Create the PowerPoint menu by clicking the General tab and typing the following text in the Caption field: PowerPoint

7. Create the Start PowerPoint menu option by double-clicking the rectangle underneath PowerPoint and typing the following text into the Caption field:Start PowerPoint

8. 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

9. 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

10. 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

11. 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

12. 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

13. Close the Menu Item Properties dialog box.

Back to the top


Step 4: Create Hooks for the Menu Option Handlers















1. In ResourceView, right-click the menu option Start PowerPoint and click ClassWizard.
2. 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.
3. 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.
4. Repeat steps 1 through 3 to create handlers for the remaining menu options. After all handlers are created, click OK to close the ClassWizard.

Back to the top


Step 5: Add the Command Handler Code












1. Follow these steps to create a member variable to hold the application object, and click OK when you are finished:













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, and click OK when you are finished:
          Variable Type: _Application
Variable Declaration: m_ppt
Access: Private
2. 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”);
}
3. 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);
}


Back to the top


Step 6: Build and Run the Application









1. 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.

2. On the Build menu, click Execute TestPPT.exe. Try selecting the different options on the PowerPoint menu.

Back to the top




APPLIES TO
















Microsoft PowerPoint 2002 Standard Edition
Microsoft PowerPoint 2000 Standard Edition
Microsoft PowerPoint 97 Standard Edition
Microsoft Foundation Class Library 4.2, when used with:










    Microsoft Visual C++ 5.0 Standard Edition
    Microsoft Visual C++ 6.0 Service Pack 5

Back to the top








Keywords:
kbprogramming kbhowto kbmacro KB169505

Back to the top


답글 남기기

이메일 주소는 공개되지 않습니다.