简体   繁体   English

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

[英]How to automatically enable Slide number footers in Powerpoint using VBA code?

I already have a macro that creates a Powerpoint presentation from Excel, but I'm having trouble enabling the Slide Number footer options in the code.我已经有一个从 Excel 创建 Powerpoint 演示文稿的宏,但我无法在代码中启用幻灯片编号页脚选项。

Specifically, I want the equivalent VBA code for inserting the slide number by doing the following in Powerpoint.具体来说,我想要等效的 VBA 代码,用于通过在 Powerpoint 中执行以下操作来插入幻灯片编号。 From the home ribbon, Go to Insert Tab > Click Header & Footer > Tick the checkbox for Slide number.从主页功能区,Go 到插入选项卡 > 单击 Header & 页脚 > 勾选幻灯片编号复选框。 This will then automatically add page numbers to all the slides so if the user changes anything like adding/removing/moving slides, the slide numbers will also subsequently change.然后,这将自动为所有幻灯片添加页码,因此如果用户更改任何内容(如添加/删除/移动幻灯片),幻灯片编号也会随之更改。

Note: Technically I could do a for loop through all the slides in Powerpoint and add textboxes on the lower left corner to add the slide numbers, but doing this, the intended user who is not tech-savvy would have to change the page number manually if they change anything in the Powerpoint because it will just be a textbox.注意:从技术上讲,我可以对 Powerpoint 中的所有幻灯片进行 for 循环,并在左下角添加文本框以添加幻灯片编号,但这样做,不精通技术的目标用户将不得不手动更改页码如果他们更改 Powerpoint 中的任何内容,因为它只是一个文本框。

Here's what I figured out so far but it's not working.到目前为止,这是我想通的,但它不起作用。

Dim PPTApp As New PowerPoint.Application
Dim PPTPres As PowerPoint.Presentation
Dim ppt_template_path As String
ppt_template_path = "C:\Users\user\Documents\Performance 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

Btw I'm using Microsoft 365 version.顺便说一句,我使用的是 Microsoft 365 版本。 So I want to know if you guys have any ideas please!所以我想知道你们是否有任何想法! I would really appreciate it!我真的很感激!

That's old code from the PowerPoint 2003 macro recorder (yes, PowerPoint used to have one.).这是来自 PowerPoint 2003 宏记录器的旧代码(是的,PowerPoint 曾经有一个。)。 Some VBA has changed since then.一些 VBA 从那以后发生了变化。 Please note the addition of Dim oSlide as Slide to the declarations:请注意在声明中添加Dim oSlide as 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:\Users\user\Documents\Performance 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

Test that the existing presentation can turn on slide numbers using Insert>Header & Footer>Slide number>Apply to All .使用Insert>Header & Footer>Slide number>Apply to All测试现有演示文稿是否可以打开幻灯片编号。 There are lots of templates out there where a designer has deleted the slide number placeholder from the master and layouts, making it impossible.有很多模板,设计者从母版和布局中删除了幻灯片编号占位符,使其无法实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM