PowerPoint VBA参考代码库1

2023-08-30 08:29:33 浏览数 (1)

下面是在PowerPoint中使用的一些基本的VBA代码,供有兴趣的朋友参考。

引用演示文档、幻灯片和形状

代码语言:javascript复制
'设置变量引用当前演示文档
Dim pptPresentation As Presentation
Set pptPresentation = ActivePresentation
'设置变量引用当前幻灯片
Dim pptSlide As Slide
Set pptSlide = Application.ActiveWindow.View.Slide
'设置变量引用指定幻灯片(第4张幻灯片)
Dim pptSlide As Slide
Set pptSlide = ActivePresentation.Slides(4)
'设置变量引用所选对象
Dim pptObject As Object
Set pptObject = ActiveWindow.Selection.ShapeRange

统计幻灯片数量

代码语言:javascript复制
'统计当前演示文档中幻灯片数量
Debug.Print ActivePresentation.Slides.Count

获取当前幻灯片的索引值

代码语言:javascript复制
'获取当前幻灯片的编号
Debug.Print Application.ActiveWindow.View.Slide.SlideIndex

添加幻灯片

代码语言:javascript复制
'在当前演示文档位置5处创建新的空白幻灯片
Dim pptSlide As Slide
Set pptSlide = ActivePresentation.Slides.Add(5, ppLayoutBlank)
'在当前演示文档的最后创建新的空白幻灯片
Dim pptSlide As Slide
Set pptSlide = ActivePresentation.Slides.Add(ActivePresentation.Slides.Count   1,
ppLayoutBlank)

代码中的ppLayoutBlank只是幻灯片布局选项之一,更多设置布局的选项值如下:

ppLayoutBlank

ppLayoutChart

ppLayoutChartAndText

ppLayoutClipartAndText

ppLayoutClipArtAndVerticalText

ppLayoutFourObjects

ppLayoutLargeObject

ppLayoutMediaClipAndText

ppLayoutMixed

ppLayoutObject

ppLayoutObjectAndText

ppLayoutObjectOverText

ppLayoutOrgchart

ppLayoutTable

ppLayoutText

ppLayoutTextAndChart

ppLayoutTextAndClipart

ppLayoutTextAndMediaClip

ppLayoutTextAndObject

ppLayoutTextAndTwoObjects

ppLayoutTextOverObject

ppLayoutTitle

ppLayoutTitleOnly

ppLayoutTwoColumnText

ppLayoutTwoObjectsAndText

ppLayoutTwoObjectsOverText

ppLayoutVerticalText

ppLayoutVerticalTitleAndText

ppLayoutVerticalTitleAndTextOverChart

删除幻灯片

代码语言:javascript复制
'删除第6张幻灯片
ActivePresentation.Slides(6).Delete

移动幻灯片

代码语言:javascript复制
'移动幻灯片的位置, 将幻灯片4移动到幻灯片2
ActivePresentation.Slides(4).MoveTo toPos:=2

0 人点赞