1.Create a dialog based application and in the App-wizard's step 3 of 6, select the automation checkbox.
2.Create buttons for Start , Run, Close, First Slide, Last Slide, Previous Slide and Next Slide functions and use the following functions accordingly.
3.In your application's InitInstance function , add the following lines.
// Initialize OLE libraries
if (!AfxOleInit())
{
AfxMessageBox("Failed to initialize OLE");
return FALSE;
}
4.In your dialog's class , open class-wizard , select the automation tab, select "Add Class" ... "From a type library" and select msppt8.olb from "C:\Program Files\Microsoft Office\Office\"
5.In your header file of your dialog, include the following line.
#include "msppt8.h"
6.Add the following variables in your dialog's header file.
_Application app; // app is the PowerPoint _Application object
9.To close PowerPoint add this code in the Close button's function call.
void CPowerPntDlg::OnBtnClose()
{
if (CanExit())
app.Quit();
}
10.To run the slideshow use this code in the Run button's function call
void CPowerPntDlg::OnBtnRun()
{
Presentations = app.GetActivePresentation();
slides = Presentation.GetSlides();
// Show the first slide of the presentation
slide = slides.Item(COleVariant((long)1));
//Run the show
slideshow = Presentation.GetSlideShowSettings();
slideshow.Run();
}
11.Sometimes, you might want to start all over from the first slide. To go to the first slide you can use this code.
13.Now that you have the slideshow running, you would obviously want to go to the previous slide at some point of time. To do just that, you can use this code.
9.To close PowerPoint add this code in the Close button's function call.
void CPowerPntDlg::OnBtnClose()
{
if (CanExit())
app.Quit();
}
10.To run the slideshow use this code in the Run button's function call
void CPowerPntDlg::OnBtnRun()
{
Presentations = app.GetActivePresentation();
slides = Presentation.GetSlides();
// Show the first slide of the presentation
slide = slides.Item(COleVariant((long)1));
//Run the show
slideshow = Presentation.GetSlideShowSettings();
slideshow.Run();
}
11.Sometimes, you might want to start all over from the first slide. To go to the first slide you can use this code.
13.Now that you have the slideshow running, you would obviously want to go to the previous slide at some point of time. To do just that, you can use this code.
void CPowerPntDlg::OnBtnStart()
{
// Start PowerPoint and get Application object...
// 启动PowerPoint ,然后获取应用程序对象...
if(!app.CreateDispatch("Powerpoint.Application"))
{
AfxMessageBox("Couldn't start PowerPoint.");
}
else // Make PowerPoint visible and display a message,让PowerPoint可见,并显示一条消息
{
app.SetVisible(TRUE);
TRACE("PowerPoint 正在运行!");
}
}
8.为了从硬盘打开演示文稿,打开按钮的函数调用(Open button's function call)中添加此代码。
void CPowerPntDlg::OnBtnOpen()
{
static char BASED_CODE szFilter[] = "PowerPoint Files (*.ppt)|*.ppt||";
CFileDialog FileDlg(TRUE,"PPT",NULL,OFN_FILEMUSTEXIST|OFN_NONETWORKBUTTON
|OFN_PATHMUSTEXIST,szFilter);
FileDlg.DoModal();
// To get the selected file's path and name
// 为了得到所选文件的路径和名称
CString strFileName;
strFileName = FileDlg.GetPathName();
if(!strFileName.IsEmpty())
{
Presentations = app.GetPresentations();
Presentation = Presentations.Open(strFileName,0,0,1);
}
}
9.为了关闭PowerPoint,在“关闭”按钮的函数调用中添加这个代码。
void CPowerPntDlg::OnBtnClose()
{
if (CanExit())
app.Quit();
}
10.为了运行幻灯片,在运行按钮的函数调用中使用此代码
void CPowerPntDlg::OnBtnRun()
{
Presentations = app.GetActivePresentation();
slides = Presentation.GetSlides();
// Show the first slide of the presentation
// 显示第一张幻灯片的演示文稿
slide = slides.Item(COleVariant((long)1));
//Run the show
// 运行。。show?就是显示吧。。
slideshow = Presentation.GetSlideShowSettings();
slideshow.Run();
}