PowerPoint-VBA代码

将活动的演示文稿保存为PDF格式

  1. Sub SavePresentationAsPDF()
  2. Dim pptName As String
  3. Dim PDFName As String
  4. '将PowerPoint另存为PDF
  5. pptName = ActivePresentation.FullName
  6. '将名称中的PowerPoint文件扩展名替换为PDF
  7. PDFName = Left(pptName, InStr(pptName, ".")) & "pdf"
  8. ActivePresentation.ExportAsFixedFormat PDFName, 2 ' ppFixedFormatTypePDF = 2
  9. End Sub

创建一个新的演示文稿

  1. Dim currentSlideIndex As Slide
  2. currentSlideIndex = Application.ActiveWindow.View.Slide.SlideIndex

打开一个现有的演示文稿

上面的代码假设演示文稿与包含该代码的PowerPoint演示文稿在同一目录下。

  1. Dim ppt As Presentation
  2. Set ppt = Presentations.Open("My Presentation.pptx")

引用活动演示文稿

当VBA代码被执行时,使用ActivePrentation来操作GUI中的当前的演示文稿对象。

  1. ' 将ActivePresentation的名称打印到即时窗口中。
  2. Debug.Print ActivePresentation.Name

保存当前演示文稿

  1. '保存当前演示文稿
  2. ActivePresentation.Save

关闭当前演示文稿

  1. '关闭当前演示文稿
  2. ActivePresentation.Close

统计幻灯片数量

  1. Dim slideCount As Long
  2. slideCount = ActivePresentation.Slides.Count

获取当前幻灯片的幻灯片序号

  1. Dim currentSlideIndex As Slide
  2. currentSlideIndex = Application.ActiveWindow.View.Slide.SlideIndex

在幻灯片末尾添加空白幻灯片

  1. Dim slideCount As Long
  2. Dim newSlide as Slide
  3. slideCount = ActivePresentation.Slides.Count
  4. Set newSlide = ActivePresentation.Slides.Add(slideCount + 1, 12)
  5. ' or as ppLayoutBlank = 12
  6. Set newSlide = ActivePresentation.Slides.Add(slideCount + 1, ppLayoutBlank)

在当前幻灯片后添加一个幻灯片

  1. Dim newSlide As Slide
  2. Dim currentSlideIndex as Integer
  3. currentSlideIndex = Application.ActiveWindow.View.Slide.SlideIndex
  4. Set newSlide = ActivePresentation.Slides.Add(currentSlideIndex, ppLayoutBlank)

删除一张幻灯片

  1. Dim currentSlideIndex as Integer
  2. currentSlideIndex = Application.ActiveWindow.View.Slide.SlideIndex
  3. ActivePresentation.Slides(currentSlideIndex).Delete

转到特定的幻灯片

  1. ' 这将把您带到第四张幻灯片
  2. Application.ActiveWindow.View.GotoSlide (4)

移动幻灯片 您可以将幻灯片从原来的位置移动到新的位置。

  1. ' 从幻灯片3移到第一张幻灯片
  2. Dim oldPosition as integer, dim newPosition as integer
  3. oldPosition = 3
  4. newPosition = 1
  5. ActivePresentation.Slides(oldPosition).MoveTo toPos:=newPosition

遍历所有幻灯片

你可以在每张幻灯片上做一些事情,也可以翻阅所有的幻灯片,找到几张幻灯片,用代码做一些事情。

  1. Dim mySlide as Slide
  2. For Each mySlide In ActivePresentation.Slides
  3. '对变量‘mySlide’中引用的当前幻灯片执行一些操作
  4. Debug.Print mySlide.Name
  5. Next Slide

遍历当前幻灯片的所有形状对象

可以通过使用 “形状 “来实现PowerPoint的威力。下面的代码将遍历当前幻灯片上的所有形状,这样你就可以按照你的要求来操作它们。

  1. Dim currentSlide as Slide
  2. Dim shp as Shape
  3. Set currentSlide = Application.ActiveWindow.View.Slide
  4. For Each shp In currentSlide.Shapes
  5. '对变量'shp'中引用的当前形状执行某些操作。
  6. '例如,在即时窗口中打印形状的名称
  7. Debug.Print shp.Name
  8. Next shp

遍历所有幻灯片中的所有形状

你可以通过添加一个循环来遍历所有幻灯片中的所有形状。

  1. Dim currentSlide as Slide
  2. Dim shp as Shape
  3. For Each currentSlide In ActivePresentation.Slides
  4. For Each shp In currentSlide.Shapes
  5. Debug.Print shp.Name
  6. 'Debug.Print 调试打印结果
  7. Next shp
  8. Next currentSlide

遍历活动幻灯片的所有文本框

文本框是PowerPoint演示文稿中最常用的形状。你可以通过添加一个 “形状类型 “的复选框,在所有的文本框中循环使用。文本框的形状类型定义为VBA常数msoTextBox(常数值为17)。

  1. Dim currentSlide as Slide
  2. Dim shp as Shape
  3. Set currentSlide = Application.ActiveWindow.View.Slide
  4. For Each shp In currentSlide.Shapes
  5. '检查形状类型是否为msoTextBox
  6. If shp.Type = 17 Then ' msoTextBox文本框 = 17
  7. '打印文本框中的文本
  8. Debug.Print shp.TextFrame2.TextRange.Text
  9. End If
  10. Next shp

遍历所有幻灯片中的所有文本框

同样,你可以通过添加一个循环来遍历所有的幻灯片。

  1. Dim currentSlide as Slide Dim shp as Shape
  2. For Each currentSlide In ActivePresentation.Slides
  3. For Each shp In currentSlide.Shapes
  4. '检查形状类型是否为msoTextBox
  5. If shp.Type = 17 Then ' msoTextBox文本框 = 17
  6. '对变量’shp‘中引用的文本框执行某些操作
  7. Debug.Print shp.TextFrame2.TextRange.Text
  8. End If
  9. Next shp
  10. Next currentSlide

将选定的幻灯片复制到新的PPT演示文稿

要将某些幻灯片复制到新的演示文稿中,首先在现有的演示文稿中选择需要的幻灯片,然后运行下面的代码。

  1. Dim currentPresentation as Presentation
  2. Dim currentSlide as Slide
  3. Dim newPresentation as Presentation
  4. '保存对当前演示文稿的引用
  5. Set currentPresentation = Application.ActivePresentation
  6. '保存对当前幻灯片的引用
  7. Set currentSlide = Application.ActiveWindow.View.Slide
  8. '添加新演示文稿并保存到引用
  9. Set NewPresentation = Application.Presentations.Add
  10. '复制选定的幻灯片
  11. Selection.Copy
  12. '粘贴到新演示文稿中
  13. NewPresentation.Slides.Paste

将当前幻灯片复制到当前演示文稿的末尾

  1. '复制当前幻灯片
  2. Application.ActiveWindow.View.Slide.Copy
  3. ' 粘贴在末尾
  4. ActivePresentation.Slides.Paste

在幻灯片放映过程中切换当前幻灯片

  1. Sub ChangeSlideDuringSlideShow()
  2. Dim SlideIndex As Integer
  3. Dim SlideIndexPrevious As Integer
  4. '在放映幻灯片时将当前幻灯片更改为选定的幻灯片4
  5. SlideIndex = 4
  6. '当前幻灯片放映窗口的索引是SlideShowWindows集合中的1
  7. SlideIndexPrevious = SlideShowWindows(1).View.CurrentShowPosition
  8. SlideShowWindows(1).View.GotoSlide SlideIndex
  9. End Sub

更改所有文本框中所有幻灯片上的字体

  1. Sub ChangeFontOnAllSlides()
  2. Dim mySlide As slide
  3. Dim shp As Shape
  4. ' 更改所有幻灯片上的字体大小
  5. For Each mySlide In ActivePresentation.Slides
  6. For Each shp In mySlide.Shapes
  7. If shp.Type = 17 Then ' msoTextBox文本框 = 17
  8. ' 将字体大小更改为24
  9. shp.TextFrame.TextRange.Font.Size = 24
  10. End If
  11. Next shp
  12. Next mySlide
  13. End Sub

将所有文本框中的大小写从大写改为正常值

  1. Sub ChangeCaseFromUppertoNormal()
  2. Dim mySlide As slide
  3. Dim shp As Shape
  4. '将所有幻灯片的大小写更改为普通大小写
  5. For Each mySlide In ActivePresentation.Slides
  6. For Each shp In mySlide.Shapes
  7. If shp.Type = 17 Then ' msoTextBox文本框 = 17
  8. ' 将大写字母更改为正常大小写
  9. shp.TextFrame2.TextRange.Font.Allcaps = False
  10. End If
  11. Next shp
  12. Next mySlide
  13. End Sub

在所有文本框的大小写在大写和正常值之间切换

  1. Sub ToggleCaseBetweenUpperAndNormal()
  2. Dim mySlide As slide
  3. Dim shp As Shape
  4. ' 在所有幻灯片的大写和普通大小写之间切换
  5. For Each mySlide In ActivePresentation.Slides
  6. For Each shp In mySlide.Shapes
  7. If shp.Type = 17 Then ' msoTextBox = 17
  8. ' 在大写和普通大小写之间切换
  9. shp.TextFrame2.TextRange.Font.Allcaps = _
  10. Not shp.TextFrame2.TextRange.Font.Allcaps
  11. End If
  12. Next shp
  13. Next mySlide
  14. End Sub

移除下划线

在字体设计中,下伸部分是指字母的基线以下的部分。在大多数字体中,下划线是为小写字母保留的,如g、j、q、p、y,有时还有f。 当你在给文字加下划线时,在下伸部分以下的文字看起来并不美观。下面是在整个演示文稿中删除g、j、p、q、y等所有此类字符下划线的代码。

  1. Sub删除下划线()
  2. Dim mySlide As Slide
  3. Dim shp As Shape
  4. Dim descenders_list As String
  5. Dim phrase As String
  6. Dim x As Long
  7. descenders_list = "gjpqy"
  8. For Each mySlide In ActivePresentation.Slides
  9. For Each shp In mySlide.Shapes
  10. If shp.Type = 17 Then ' msoTextBox文本框 = 17
  11. ' 去掉字母“gjpqy”中的下划线
  12. With shp.TextFrame.TextRange
  13. phrase = .Text
  14. For x = 1 To Len(.Text)
  15. If InStr(descenders_list, Mid$(phrase, x, 1)) > 0 Then
  16. .Characters(x, 1).Font.Underline = False
  17. End If
  18. Next x
  19. End With
  20. End If
  21. Next shp
  22. Next mySlide
  23. End Sub

从所有幻灯片中删除动画

使用下面的代码来删除演示文稿中设置的所有动画。

  1. Sub RemoveAnimationsFromAllSlides()
  2. Dim mySlide As slide
  3. Dim i As Long
  4. For Each mySlide In ActivePresentation.Slides
  5. For i = mySlide.TimeLine.MainSequence.Count To 1 Step -1
  6. '从最后倒数删除每个动画
  7. mySlide.TimeLine.MainSequence.Item(i).Delete
  8. Next i
  9. Next mySlide
  10. End Sub

保存演示文稿为PDF

您可以轻松地将Active Presentation保存为PDF格式。

  1. Sub SavePresentationAsPDF()
  2. Dim pptName As String
  3. Dim PDFName As String
  4. '活动演示文稿全名赋值给变量
  5. pptName = ActivePresentation.FullName
  6. ' 将名称中的PowerPoint文件扩展名替换为PDF
  7. PDFName = Left(pptName, InStr(pptName, ".")) & "pdf"
  8. ActivePresentation.ExportAsFixedFormat PDFName, 2 ' ppFixedFormatTypePDF = 2
  9. End Sub

查找和替换文本

你可以在所有幻灯片的所有文本框中查找和替换文本。在你要查找的文本的第一个实例(由findWhat定义)之后,你需要通过查找命令循环查找其他实例(如果有的话)。

  1. Sub FindAndReplaceText()
  2. Dim mySlide As slide
  3. Dim shp As Shape
  4. Dim findWhat As String
  5. Dim replaceWith As String
  6. Dim ShpTxt As TextRange
  7. Dim TmpTxt As TextRange
  8. findWhat = "jackal"
  9. replaceWith = "fox"
  10. ' 查找、查找和替换
  11. For Each mySlide In ActivePresentation.Slides
  12. For Each shp In mySlide.Shapes
  13. If shp.Type = 17 Then ' msoTextBox = 17
  14. Set ShpTxt = shp.TextFrame.TextRange
  15. '查找“Find”单词的第一个实例(如果存在)
  16. Set TmpTxt = ShpTxt.Replace(findWhat, _
  17. Replacewhat:=replaceWith, _
  18. WholeWords:=True)
  19. '查找“Find”单词的任何其他实例(如果存在)
  20. Do While Not TmpTxt Is Nothing
  21. Set ShpTxt = ShpTxt.Characters(TmpTxt.Start + TmpTxt.Length, ShpTxt.Length)
  22. Set TmpTxt = ShpTxt.Replace(findWhat, _
  23. Replacewhat:=replaceWith, _
  24. WholeWords:=True)
  25. Loop
  26. End If
  27. Next shp
  28. Next mySlide
  29. End Sub

导出幻灯片为图片

您可以将Current SLide(或任何其他幻灯片)导出为PNG或JPG(JPEG)或BMP图像。

  1. Sub ExportSlideAsImage()
  2. Dim imageType As String
  3. Dim pptName As String
  4. Dim imageName As String
  5. Dim mySlide As slide
  6. ' 将当前幻灯片导出为图像
  7. imageType = "png" ' or jpg or bmp
  8. pptName = ActivePresentation.FullName
  9. imageName = Left(pptName, InStr(pptName, ".")) & imageType
  10. Set mySlide = Application.ActiveWindow.View.slide
  11. mySlide.Export imageName, imageType
  12. End Sub

调整图像大小以覆盖整个幻灯片

  1. Sub ResizeImageToCoverFullSlide()
  2. Dim mySlide As slide
  3. Dim shp As Shape
  4. '将图像大小调整为完整幻灯片大小。
  5. '更改当前幻灯片上第一个形状的高度和宽度。
  6. '以适应幻灯片尺寸
  7. Set mySlide = Application.ActiveWindow.View.slide
  8. Set shp = mySlide.Shapes(1)
  9. '如果要展开当前选定的形状,将上面的两个语句替换为以下语句。
  10. 'Set shp = ActiveWindow.Selection.ShapeRange(1)
  11. '如果未选定任何内容,则会显示错误
  12. With shp
  13. .LockAspectRatio = False
  14. .Height = ActivePresentation.PageSetup.SlideHeight
  15. .Width = ActivePresentation.PageSetup.SlideWidth
  16. .Left = 0
  17. .Top = 0
  18. End With
  19. End Sub

退出所有运行中的幻灯片放映

如果你有多个SlideShow同时打开,那么你可以使用下面的宏关闭所有的SlideShow。

  1. Sub ExitAllRunningSlideShows()
  2. Do While SlideShowWindows.Count > 0
  3. SlideShowWindows(1).View.Exit
  4. Loop
  5. End Sub

从Excel自动化操作PowerPoint

您还可以通过其他应用程序(如Excel和Word)连接到PowerPoint。作为第一步,你必须引用一个PowerPoint的实例。
有两种方法可以做到这一点 - 早期绑定和后期绑定。

打开PowerPoint - 早期绑定

在 “早期绑定 “中,您必须在VBE(Visual Basic Editor)中使用 “工具->引用 “选项,显式设置 “Microsoft PowerPoint 16对象库”(适用于MS Office 2019)。
‘ Early Binding
Dim pptApp As Application
Set pptApp = New PowerPoint.Application

打开PowerPoint - 后期绑定

在 “后期绑定 “中,应用程序变量被声明为对象,VBA引擎在运行时连接到正确的应用程序。
‘ Late Binding
Dim pptApp As Object
Set pptApp = CreateObject(“PowerPoint.Application”)

使应用可见

在设置PowperPoint应用程序的引用后,你可能需要使其可见。
pptApp.Visible = True

操作PowerPoint

你可以从Excel使用前面描述的所有的从PowerPoint中的方法来操作演示文稿,只需添加对你上面创建的PowerPoint的引用。
举例来说
Presentations.Open (“My Presentation.pptx”)
需要这样使用
pptApp .Presentations.Open (“My Presentation.pptx”)

关闭应用程序

一旦你完成了你想做的PowerPoint应用程序,你必须关闭它,并应释放参考。
pptApp.Quit
Set pptApp = Nothing

从Excel复制到PowerPoint

此代码将从Excel复制一个范围到PowerPoint。
注意:为了展示如何使用VBA将一个范围从Excel复制到PowerPoint中,它尽可能地保持简单。

  1. Sub copyRangeToPresentation()
  2. '打开新的PowerPoint实例
  3. Set pptApp = CreateObject("PowerPoint.Application")
  4. With pptApp
  5. '创建新演示文稿
  6. Set ppt = .Presentations.Add
  7. ' 添加空白幻灯片
  8. Set newSlide = ppt.Slides.Add(1, 12) ' ppLayoutBlank = 12
  9. ' 在Excel中从活动工作表复制区域
  10. ActiveSheet.Range("A1:E10").Copy
  11. ' 粘贴到Powerpoint作为图像
  12. newSlide.Shapes.PasteSpecial DataType:=2 '2=PP粘贴增强型元文件
  13. ' 切换到PowerPoint
  14. .Activate
  15. End With
  16. End Sub