1.添加ppt类型库文件,"msppt.olb"(office 2003中)。
2.头文件 #include “msppt.h”。
3.在应用程序中,InitInstance函数中,添加
if (!AfxOleInit())
{
AfxMessageBox("Failed to initialize OLE");
return FALSE;
}
4.In header file中,
_Application app; //app is the PowerPoint _Application object
Presentations Presentations; //PPT环境
_Presentation Presentation; //打开的PPT文件
SlideShowView View;
SlideShowWindow SlideShowWindow;
SlideShowSettings slideshow;
Slides slides;
_Slide slide;
5.启动PowerPoint,并获得应用程序对象。
if(!app.CreateDispatch("Powerpoint.Application"))
{
AfxMessageBox("Couldn't start PowerPoint.");
}
else // Make PowerPoint visible and display a message
{
app.SetVisible(TRUE);
TRACE("PowerPoint is Running!");
}
6.打开一个ppt文件
static char BASED_CODE szFilter[] = "PowerPoint Files (*.ppt)|*.ppt||";
CFileDialog FileDlg(TRUE,”PPT”,NULL,OFN_FILEMUSTEXIST |OFN_NONETWORKBUTTON | OFN_PATHMUSTEXIST,szFilter);
FileDlg.DoModal();//打开“打开文件”对话框
Cstring strFileName;
strFileName = FileDlg.GetPathName(); //获得选择的文件和路径名
if(!strFileName.IsEmpty())
{
Presentations = app.GetPresentations(); //
Presentation = Presentations.Open(strFileName,0,0,1); //打开ppt文件
}
7.关闭
documentwindow=app.GetActiveWindow();//获得活动的文档
documentwindow.Close();//关闭当前活动的文档
if(CanExit())
app.Quit(); //关闭运行环境
8.运行幻灯片
Presentations = app.GetActivePresentation(); //获得当前活动的窗体
slides = Presentation.GetSlides(); //获得当前活动ppt文件的幻灯片
// Show the first slide of the presentation
slide = slides.Item(COleVariant((long)1)); //显示第一张幻灯片
//Run the show
slideshow = Presentation.GetSlideShowSettings(); //获得幻灯片的设置
slideshow.Run(); //播放幻灯片
9.播放第一个或最后一个幻灯片
Presentation = app.GetActivePresentation();
SlideShowWindow = Presentation.GetSlideShowWindow();
View = SlideShowWindow.GetView();
View.First(); //定位到第一个幻灯片
View.Last(); //定位到最后一个幻灯片
View.Previous();//定位到上一个幻灯片
View.Next(); //定位到下一个幻灯片
10.获得幻灯片总数
Presentations=app.GetActivePresentation();
slides=Presentation.GetSlides();
long endpos=slides.GetCount(); //获得幻灯片总数
11.获得当前幻灯片序号
Presentation=app.GetActivePresentation();
SlideShowWindow=Presentation.GetSlideShowWindow();
View=SlideShowWindow.GetView();
long Curpos=View.GetCurrentShowPosition();//当前幻灯片序号