这里可以使用下面的PowerPointSlideshow宏从Excel中运行PowerPoint演示文稿。幻灯片放映完成后,可以按Esc键或单击屏幕上的任意位置来显示仍打开的演示文稿。在宏中已放置了注释,以解释代码在执行过程中的作用,并指出可以在何处调整PowerPoint演示文稿的名称、路径位置和幻灯片放映时间。
完整的VBA代码如下:
Sub PowerPointSlideshow()
'声明并定义字符串变量(可根据需要修改).
Dim strFilePath As String
Dim strFileName As String
Dim strFilePathAndName
strFilePath = ThisWorkbook.Path & "\"
strFileName = "SlideshowTest.pptx"
strFilePathAndName = strFilePath & strFileName
'验证声明的路径中是否存在声明的PowerPoint演示文稿名称
If Len(Dir(strFilePathAndName, vbDirectory)) = 0 Then
MsgBox "没有文件名''" & strFileName & "''" & vbCrLf & _
"在路径 ''" &strFilePath & "''中." & vbCrLf & vbCrLf& _
"请核对PPT" &vbCrLf & "名称, 以及在路径" &vbCrLf & _
strFilePath & " 中的位置.",48, "不能继续 -- 没有找到文件名和路径."
Exit Sub
End If
'声明和定义对象变量.
Dim ppApp As Object
Dim ppPres As Object
Set ppApp = CreateObject("PowerPoint.Application")
Set ppPres = ppApp.Presentations.Open(strFilePath & strFileName)
'确定幻灯片的观看时间.
'本示例为5秒.
With ppPres.slides.Range.slideshowtransition
.advanceontime = True
.advancetime = 5
End With
'运行幻灯展示.
ppPres.slideshowsettings.Run
'表示PowerPoint演示文稿已保存为真,
'如果想在不提示保存的情况下关闭它.
ppPres.Saved = True
'从内存中释放对象变量.
Set ppPres = Nothing
Set ppApp = Nothing
End Sub
有兴趣的朋友可以试试!
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有