一、PowerPoint vba开发对象详解:
1、 Application对象
该对象代表PowerPoint应用程序,通过该对象可访问PowerPoint中的其他所有对象。
(1)Active属性:返回指定窗格是否被激活。
(2)ActivePresentation属性:返回Presentation对象,代表活动窗口中打开的演示文稿。
(3)ActiveWindow属性:返回DocumentWindow对象,代表当前文档窗口。
(4)Presentations属性:返回Presentations集合,代表所有打开的演示文稿。
(5)SlideShowWindows属性:返回 SlideShowWindows集合,代表所有打开的幻灯片放映窗口。
(6)Quit方法:用于退出PowerPoint程序。
2、 DocumentWindow对象
该对象代表文档窗口。使用“Windows(index) ”语法可返回DocumentWindow对象。
(1)ActivePane属性:返回Pane对象,代表文档窗口中的活动窗格。
(2)Panes属性:返回Panes集合,代表文档窗口中的所有窗格。
(3)ViewType属性:返回指定的文档窗口内的视图类型。
3、 Presentation对象
该对象代表演示文稿,通过“Presentations(index)”语法可返回Presentation对象。
(1)BuiltInDocumentProperties属性:返回DocumentProperties集合,代表演示文稿的所有文档属性。
(2)ColorSchemes属性:返回ColorSchemes 集合,代表演示文稿的配色方案。
(3)PageSetup属性:返回PageSetup对象,用于控制演示文稿的幻灯片页面设置属性。
(4)SlideMaster属性:返回幻灯片母版对象。
(5)SlideShowSettings属性:返回SlideShowSettings对象,代表演示文稿的幻灯片放映设置。
(6)SlideShowWindow属性:返回幻灯片放映窗口对象。
(7)AddTitleMaster方法:为演示文稿添加标题母版。
(8)ApplyTemplate方法:对演示文稿应用设计模板。
4、 SlideShowWindow对象
该对象代表幻灯片放映窗口。
IsFullScreen属性:用于设置是否全屏显示幻灯片放映窗口。
5、 Master对象
该对象代表幻灯片母版、标题母版、讲义母版或备注母版。
TextStyles属性:为幻灯片母版返回TextStyles 集合,代表标题文本、正文文本和默认文本。
6、 Slide对象
该对象代表幻灯片。
(1)SlideID属性:返回幻灯片的唯一标识符。
(2)SlideIndex属性:返回幻灯片在Slides集合中的索引号。
7、 SlideShowView对象
该对象代表幻灯片放映窗口中的视图。
(1)AcceleratorsEnabled属性:用于设置是否允许在幻灯片放映时使用快捷键。
(2)CurrentShowPosition属性:返回当前幻灯片在放映中的位置。
(3)DrawLine方法:在指定幻灯片放映视图中绘制直线。
(4)EraseDrawing方法:用于清除通过DrawLine方法或绘图笔工具在放映中绘制的直线。
(5)GotoSlide方法:用于切换指定幻灯片。
二、编程方法:
在程序中加入Servers面板中的TPowerPointApplication、TPowerPointPresentation、TPowerPointSlide三个组件
在Uses中,加入PowerPointXP,Office2000,ComObj单元
以下为几句常用的语句:
(01)建立连接:
PowerPointApplication1.Connect;
(02)打开文件:
PowerPointApplication1.Presentations.Open(sFilename, 0, 0,
0);
(03)新建文件:
PowerPointApplication1.Presentations.add(0);
(04)演示文稿:
PowerPointPresentation1.ConnectTo(PowerPointApplication1.Presentations.Item(1));
(05)幻灯片数量:
PowerPointPresentation1.Slides.Count;
//1表示为第1张幻灯片
(06)处理幻灯片:
PowerPointSlide1.ConnectTo(PowerPointPresentation1.Slides.Item(1));
//1表示为第1张幻灯片
(07)处理文本:
PowerPointSlide1.Shapes.Item(1).TextFrame.TextRange.text :=
'3213';
(08)处理表格:
PowerPointSlide1.Shapes.Item(2).Table.Cell(1,
2).Shape.TextFrame.TextRange.Text := '2121';
(09)插入图片:
PowerPointSlide1.Shapes.AddPicture(sFileName, 1, 1, 100, 170, 520,
200); //后面的参数为位置
(10)文件存盘:
PowerPointPresentation1.Save;
(11)文件另存为:
PowerPointPresentation1.SaveAs(ChangeFileExt(MyFilename, '.jpg'),
ppSaveAsJPG, 0); //文件另存为Jpg文件
(12)关闭文件:
PowerPointPresentation1.Close;
(13)退出实例:
PowerPointApplication1.Quit;
(14)取消连接:
PowerPointApplication1.Disconnect;
三、编程实例:
1、用VBA宏代码将PPT文件中,每个幻灯片拆分成一个单独PPT文件:
打开PPT文件,同时按下Alt键和F11键,新建一个模块,代码如下:
Option Explicit
Sub SplitSlides()
Dim oSrcPresentation As Presentation, oNewPresentation As
Presentation
Dim strSrcFileName As String, strNewFileName As String
Dim nIndex As Integer, nSubIndex As Integer, nTotalSlides As
Integer, nBound As Integer, nCounter As Integer
Dim fso As Object
Const nSteps = 1 ' 这个参数是控制每隔几页分割成一个文件
If nSteps <= 0 Then Exit Sub
Set fso = CreateObject("Scripting.FileSystemObject")
Set oSrcPresentation = ActivePresentation
strSrcFileName = oSrcPresentation.FullName
nTotalSlides = oSrcPresentation.Slides.Count
nCounter = 1
For nIndex = 1 To nTotalSlides Step nSteps
If nIndex + nSteps > nTotalSlides Then
nBound =
nTotalSlides
Else
nBound =
nIndex + nSteps - 1
End If
strNewFileName =
fso.BuildPath(fso.GetParentFolderName(strSrcFileName), _
fso.GetBaseName(strSrcFileName) & "_" & nCounter & "."
& fso.GetExtensionName(strSrcFileName))
oSrcPresentation.SaveCopyAs strNewFileName
Set oNewPresentation = Presentations.Open(strNewFileName)
If nBound < nTotalSlides Then
For nSubIndex = nBound + 1 To nTotalSlides
oNewPresentation.Slides(nBound + 1).Delete
Next
End If
If nIndex > 1 Then
For nSubIndex = 1 To nIndex - 1
oNewPresentation.Slides(1).Delete
Next
End If
oNewPresentation.Save
oNewPresentation.Close
nCounter = nCounter + 1
Next nIndex
MsgBox "结束!", vbInformation
End Sub
最后按F5键,运行这段宏代码即可。
注意的是:若文件不允许运行宏,只需将宏的安全性设置为最低级别即可。
2、自动播放幻灯片。本例利用定时器来实现自动播放幻灯片
type
ppta: TPowerPointApplication;
pptp: TPowerPointPresentation;
procedure
TForm1.FormCreate(Sender: TObject);
var
ssSet:
SlideShowSettings;
ssWin:
SlideShowWindow;
begin
ppta
:=TPowerPointApplication.Create(self);
ppta.Visible :=msotrue;
pptp.ConnectTo(ppta.Presentations.Open('e:\mylove.ppt',msoFalse,msoFalse,msoTrue));
ssset :=pptp.SlideShowSettings;
ssset.LoopUntilStopped :=msofalse;
ssset.ShowType := ppShowTypeSpeaker;
ssset.Run;
sswin :=pptp.SlideShowWindow;
end;
procedure
TForm1.Timer1Timer(Sender: TObject);
var
endpos,curpos :integer;
begin
endpos :=pptp.Slides.Count;
curpos :=sswin.View.CurrentShowPosition;
if curpos< endpos then
sswin.View.Next;
end;
3、实例部分代码:
(1)、实现Powerpoint演示文稿的创建及幻灯片插入、删除和移动等操作:
PP2000.Caption:='Microsoft PowerPoint';
//PPT使Powerpoint 窗口标题为Microsoft PowerPoint
Presentation:=PP2000.Presentations.add ;
//PPT创建一个新演示文稿,其中Presentation 定义为variant 类型变量;
Presentation:=PP2000.Presentations.open('c1.ppt') ;
//PPT打开指定的Powerpoint 演示文稿;
Presentation.slides.add(1,ppLayoutTitleOnly) ;
//PPT在第1张幻灯片之后插入一张新幻灯片,其中ppLayoutTitleOnly 为幻灯片版式;
Presentation.slides.add(Presentation.slides.count + 1 ,
ppLayoutTitleOnly) ; //PPT在最后一张新幻灯片之后插入一张幻灯片;
Presentation.slides.item(1).delete; //PPT删除第1 张幻灯片
Presentation.slides.item(1).cut; //PPT将第1
张幻灯片剪切到剪贴板
Presentation.slides.Paste(4);
//PPT将剪贴板中的幻灯片插到第4 张幻灯片之后
(2)、实现对Powerpoint幻灯片对象的属性值设置、修改和读取操作:
Label1.caption : = Presentation.TemplateName ;PP读取演示文稿的模板名称;
Presentation.slides.item(1).layout: = ppLayoutChart
; //PPT设置幻灯片版式;
Presentation.slides.item(1).Shapes.Title.TextFrame.TextRange.Text :
='欢迎使用'; //PPT设置第1
张幻灯片的标题为“欢迎使用”;
Presentation.slides.item(1).Shapes.Title.TextFrame.TextRange.font.name
: ='隶书'; //PPT设置第1
张幻灯片的标题字体为“隶书”;
Presentation.slides.item(1).Shapes.Title.TextFrame.TextRange.font.size
: = 40 ; //PPT设置第1 张幻灯片的标题字号为40
;
presentation.slides.item(1).Shapes.item(1).ActionSettings.item(1).Hyperlink.Address
: ='http://www.edu.cn';
//PPT设置第1张幻灯片的第1个对象的超级链
(3)、实现Powerpoint幻灯片的动画效果及切换方式的设置:
presentation.slides.item(1).Shapes.item(1).AnimationSettings.TextLevelEffect
: = ppAnimateBy2FirstLevel; //PPT设置第1张幻灯片的第1个对象由该列表第一级段落激活;
presentation.slides.item(1).Shapes.item(1).AnimationSettings.EntryEffect
: = ppEffectFly2FromTopRight;
//PPT设置第1张幻灯片的第1个对象的动画效果是“从右上角飞入”;
presentation.slides.item(1).Shapes.item(1).AnimationSettings.AfterEffect
: = ppAfterEffectDim;
//PPT设置第1张幻灯片的第1个对象的动画后变暗;
presentation.slides.item(1).Shapes.item(1).AnimationSettings.DimColor.RGB
: = RGB(100,120,100);
//PPT设置第1张幻灯片的第1个对象的动画后变暗为指定颜色
presentation.slides.item(1).Shapes.item(1).AnimationSettings.AnimateTextInReverse
: = True ;
//PPT设置第1张幻灯片的第1个对象的动画内容出现按逆序出现;
presentation.slides.item(1).SlideShowTransition.Speed : =
ppTransitionSpeedFast;
//PPT设置第1张幻灯片的换页速度为“快速”;
presentation.slides.item(1).SlideShowTransition.EntryEffect : =
ppEffectStripsDownLeft;
//PPT设置第1张幻灯片的过渡方式为“阶梯状往下左扩展”方式;
presentation.slides.item(1).SlideShowTransition.AdvanceOnTime : =
True; //PPT设置第1张幻灯片在指定时间后自动换片;
presentation.slides.item(1).SlideShowTransition.AdvanceTime : =
2;
//PPT设置在第1张幻灯片放映2s后自动切换幻灯片;
(4)、实现Powerpoint 幻灯片的放映操作:
Presentation.SlideShowSettings.Run ;
//若不设置任何参数,将放映全部
//下面是放映其中一部分:
Presentation.SlideShowSettings.RangeType : =
ppShowSlideRange;
//PPT设置要放映的幻灯片放映的类型,本例中的类型为指定放映范围.
Presentation.SlideShowSettings.StartingSlide : =
1; //PPT设置从第1 张开始放映幻灯片;
Presentation.SlideShowSettings.EndingSlide : = 5;
//PPT设置第5 张为结束幻灯片;
Presentation.SlideShowSettings.AdvanceMode : =
ppSlideShowUseSlideTimings;
//PPT设置一个指示幻灯片放映方式的值;本例中设置可以使幻灯片间隔设置在整个幻灯片放映中有效.
Presentation.SlideShowSettings.LoopUntilStopped : =
True; //PPT设置指定的幻灯片循环放映直到用户按ESC键;
Presentation.SlideShowSettings.Run;
//PPT按以上设定的幻灯片放映参数值开始放映幻灯片;
加载中,请稍候......