如何使用VBA代码在Powerpoint中自动启用幻灯片数字页脚?



我已经有了一个从Excel创建Powerpoint演示文稿的宏,但是我在代码中启用幻灯片编号页脚选项时遇到了问题。

具体来说,我需要等效的VBA代码,通过在Powerpoint中执行以下操作来插入幻灯片号。在home ribbon中,选择Insert Tab>点击Header &页脚的在勾选"幻灯片编号"复选框。这将自动为所有幻灯片添加页码,因此,如果用户更改任何内容,如添加/删除/移动幻灯片,幻灯片编号也将随之更改。

注意:从技术上讲,我可以对Powerpoint中的所有幻灯片进行for循环,并在左下角添加文本框来添加幻灯片编号,但是这样做,如果不懂技术的目标用户更改了Powerpoint中的任何内容,则必须手动更改页码,因为它将只是一个文本框。

这是我到目前为止发现的,但它不起作用。

Dim PPTApp As New PowerPoint.Application
Dim PPTPres As PowerPoint.Presentation
Dim ppt_template_path As String
ppt_template_path = "C:UsersuserDocumentsPerformance Report Template.pptx"
PPTApp.Activate
Set PPTPres = PPTApp.Presentations.Open(ppt_template_path)
'---------this is where nothing happens when I try to enable Slide Number footers
If PPTPres.HasTitleMaster Then
With PPTPres.TitleMaster.HeadersFooters
.SlideNumber.Visible = msoTrue
End With
End If
With PPTPres.SlideMaster.HeadersFooters
.SlideNumber.Visible = msoTrue
End With
With PPTPres.Slides.Range.HeadersFooters
.SlideNumber.Visible = msoTrue
End With

顺便说一句,我用的是Microsoft 365版本。所以我想知道你们是否有什么想法!我真的很感激!

这是PowerPoint 2003宏记录器的旧代码(是的,PowerPoint曾经有一个!)从那时起,一些VBA发生了变化。请注意添加了Dim Slide作为Slide到声明:

Dim PPTApp As New PowerPoint.Application
Dim PPTPres As PowerPoint.Presentation
Dim ppt_template_path As String
Dim oSlide As Slide
ppt_template_path = "C:UsersuserDocumentsPerformance Report Template.pptx"
PPTApp.Activate
Set PPTPres = PPTApp.Presentations.Open(ppt_template_path)
If PPTPres.HasTitleMaster Then
PPTPres.TitleMaster.HeadersFooters.SlideNumber.Visible = msoTrue
End If
PPTPres.SlideMaster.HeadersFooters.SlideNumber.Visible = msoTrue
For each oSlide in PPTPres.Slides
oSlide.HeadersFooters.SlideNumber.Visible = msoTrue
Next oSlide

测试现有演示文稿是否可以使用Insert>Header &适用于所有。在很多模板中,设计师从母版和布局中删除了幻灯片编号占位符,使之无法实现。

最新更新