对ppt文件的一些简单操作

本文详细介绍了如何通过OLE技术与PowerPoint API进行交互,包括添加库文件、初始化OLE、创建PowerPoint应用程序、打开和关闭PPT文件、播放幻灯片以及获取关键信息等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.添加ppt类型库文件,"msppt.olb"(office 2003中)。

2.头文件 #include “msppt.h”

3.在应用程序中,InitInstance函数中,添加

if (!AfxOleInit())

{

AfxMessageBox("Failed to initialize OLE");

return FALSE;

}

4In header file中,

_Applicationapp;//app is the PowerPoint _Application object

PresentationsPresentations;//PPT环境

_PresentationPresentation;//打开的PPT文件

SlideShowViewView;

SlideShowWindowSlideShowWindow;

SlideShowSettingsslideshow;

Slidesslides;

_Slideslide;

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();//当前幻灯片序号

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值