HOWTO: Creating an MFC Automation Controller for PowerPoint 97ID: Q169505
|
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:
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.
- _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.
OnPowerpointStartpowerpoint
Click OK to accept this name.
Variable Type: _Application
Variable Declaration: m_ppt
Access: Private
Click OK when you are finished.
CMainFrame::CMainFrame()
{
// Create an instance of the PowerPoint application.
m_ppt.CreateDispatch("PowerPoint.Application.8");
}
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);
}
Additional query words: ppt8 vba vbe
Keywords : kbmacro kbole kbMFC kbVC500 kbVC600
Version : WINDOWS:97; WINDOWS NT:5.0,6.0
Platform : NT WINDOWS
Issue type : kbhowto
Last Reviewed: August 3, 1999