文章背景:在工作中,有时遇到一份ppt,顺序正好是乱的。现在想要将最后一页放在开头,倒数第二页放在开头第二页...。如果一份ppt有多页幻灯片,手动操作特别费劲。在网上查阅资料后,发现可以通过宏命令来实现批量操作,下面介绍两种方式。
方法一:PPT VBA
此方法要求ppt的文件格式为pptm,从而支持宏的运行。在Module1
中添加如下代码:
Option Explicit
Sub reversi()
'from: https://groups.google.com/g/microsoft.public.powerpoint/c/Cbr_aDwO3Kw?pli=1
Dim opres As Presentation
Dim i As Integer
Set opres = ActivePresentation
'Cannot alter read only presentation
If opres.ReadOnly = True Then Exit Sub
'seqentially move slides to front
For i = 2 To opres.Slides.Count
opres.Slides(i).MoveTo 1
Next i
MsgBox "Done!"
End Sub
操作演示:http://mpvideo.qpic.cn/0bf2iaab4aaal4aldoptsvqfaqgddzaaahqa.f10002.mp4?dis_k=ba98367d24bd8937863b3559cc499421&dis_t=1663657098&vid=wxv_1962775642450591747&format_id=10002&support_redirect=0&mmversion=false
方法二:Excel VBA
在Module1
中添加如下代码:
Option Explicit
Sub reversi()
'逆序排列代码, from: https://groups.google.com/g/microsoft.public.powerpoint/c/Cbr_aDwO3Kw?pli=1
Dim filepath As String
Dim pptApp As Object 'PowerPoint.Application
Dim pptPres As Object 'PowerPoint.Presentation
Dim i As Integer
'1 打开ppt文件
filepath = Range("C3").Value
Shell "POWERPNT.EXE " & filepath, vbMaximizedFocus 'vbNormalFocus
'2 幻灯片逆序排列
Set pptApp = CreateObject("Powerpoint.Application")
If pptApp Is Nothing Then
Application.ActivateMicrosoftApp xlMicrosoftPowerPoint
End If
Set pptPres = pptApp.ActivePresentation
'Cannot alter read only presentation
If pptPres.ReadOnly = True Then Exit Sub
'seqentially move slides to front
For i = 2 To pptPres.Slides.Count
pptPres.Slides(i).MoveTo 1
Next i
MsgBox "Done!"
End Sub
上述代码的设计思路是:先打开ppt文件,再进行逆序排列。将reversi
宏代码赋给逆序排列
按钮。
运行效果展示:http://mpvideo.qpic.cn/0bf2baab4aaaomaldghtsvqfacgddyeaahqa.f10002.mp4?dis_k=a1380b6b410d8c640504c630f12d2289&dis_t=1663657098&vid=wxv_1962777180837412868&format_id=10002&support_redirect=0&mmversion=false
参考资料:
[1] VBA macro error (https://answers.microsoft.com/en-us/msoffice/forum/all/vba-macro-error/b1c3f55d-b69d-438d-8001-93c0417aab21)
[2] VBA Code to copy all shapes from Powerpoint to Body on email/outlook as picture (https://chandoo.org/forum/threads/vba-code-to-copy-all-shapes-from-powerpoint-to-body-on-email-outlook-as-picture.41916/)
[3] Sub to reverse slides order (https://groups.google.com/g/microsoft.public.powerpoint/c/Cbr_aDwO3Kw?pli=1)
[4] Powerpoint automation from Excel (https://answers.microsoft.com/en-us/msoffice/forum/all/powerpoint-automation-from-excel/4f509b1b-d43e-40b6-81da-26543347b9e9)
[5] Powerpoint VBA to switch back to powerpoint from Excel (https://stackoverflow.com/questions/46217042/powerpoint-vba-to-switch-back-to-powerpoint-from-excel)
[6] Excel to PPTX (http://eileenslounge.com/viewtopic.php?f=27&t=8631&sid=8c4c5eebd3c381876353089da3ba45f4)
相关阅读:
[1] Excel: 通过VBA代码打开ppt文件