使用 VBA 在 PowerPoint 中自动为幻灯片编号
Automatic slide numbering in PowerPoint using VBA
我在 PowerPoint 2013 中使用以下代码添加了一个标签以在所有幻灯片的底部显示 "x of y",其中 x 是当前幻灯片编号,y 是幻灯片总数:
Private Sub Label1_Click()
Dim p1 As String
p1 = " of "
Dim p2 As Integer
Dim slideNumber As String
slideNumber = ActiveWindow.Selection.SlideRange.slideNumber
p2 = ActivePresentation.Slides.Count
Label1.Caption = slideNumber & p1 & p2
End Sub
该代码非常适合我在其上添加标签的幻灯片,例如它在我总共 29 张幻灯片中的第 9 张幻灯片中显示“9 of 29”,但是当我将标签复制并粘贴到其他幻灯片时,它仍然显示“9 of 29”,这是错误的,因为我希望它自动反映当前幻灯片编号.
如果您希望它适用于所有幻灯片,使用一个按钮,其点击一次更新所有幻灯片编号不是更简单吗?
假设您在每张幻灯片上都有一个名为 "SlideNumber" 的形状:
Sub ForExample()
Dim oSl As Slide
For Each oSl In ActivePresentation.Slides
With oSl.Shapes("SlideNumber").TextFrame.TextRange
.Text = "Slide " & CStr(oSl.SlideIndex) & " of " _
& CStr(ActivePresentation.Slides.Count)
End With
Next
End Sub
这是我的解决方案。如果不存在则添加一个新的文本框;否则使用现有的。这样,您可以在更改幻灯片后重新运行宏。
Sub SlideNum()
For Each oSl In ActivePresentation.Slides
exist = False
For Each oS in Osl.Shapes
If oS.name = "SlideNum" Then
exist = True
Exit For
End If
Next
If exist = False Then
' convert from inch *72
Set oshp = oSl.Shapes.AddTextbox(msoTextOrientationHorizontal, 25.55*72, 14.21*72, 1.12*72, 20)
oshp.name = "SlideNum"
Else
Set oshp = oS
End If
With oshp.TextFrame.TextRange
' .Text = CStr(oSl.SlideIndex) & "/" & CStr(ActivePresentation.Slides.Count)
.Text = CStr(oSl.SlideIndex)
.Font.Size = 40
End With
Next
结束子
我在 PowerPoint 2013 中使用以下代码添加了一个标签以在所有幻灯片的底部显示 "x of y",其中 x 是当前幻灯片编号,y 是幻灯片总数:
Private Sub Label1_Click()
Dim p1 As String
p1 = " of "
Dim p2 As Integer
Dim slideNumber As String
slideNumber = ActiveWindow.Selection.SlideRange.slideNumber
p2 = ActivePresentation.Slides.Count
Label1.Caption = slideNumber & p1 & p2
End Sub
该代码非常适合我在其上添加标签的幻灯片,例如它在我总共 29 张幻灯片中的第 9 张幻灯片中显示“9 of 29”,但是当我将标签复制并粘贴到其他幻灯片时,它仍然显示“9 of 29”,这是错误的,因为我希望它自动反映当前幻灯片编号.
如果您希望它适用于所有幻灯片,使用一个按钮,其点击一次更新所有幻灯片编号不是更简单吗?
假设您在每张幻灯片上都有一个名为 "SlideNumber" 的形状:
Sub ForExample()
Dim oSl As Slide
For Each oSl In ActivePresentation.Slides
With oSl.Shapes("SlideNumber").TextFrame.TextRange
.Text = "Slide " & CStr(oSl.SlideIndex) & " of " _
& CStr(ActivePresentation.Slides.Count)
End With
Next
End Sub
这是我的解决方案。如果不存在则添加一个新的文本框;否则使用现有的。这样,您可以在更改幻灯片后重新运行宏。
Sub SlideNum()
For Each oSl In ActivePresentation.Slides
exist = False
For Each oS in Osl.Shapes
If oS.name = "SlideNum" Then
exist = True
Exit For
End If
Next
If exist = False Then
' convert from inch *72
Set oshp = oSl.Shapes.AddTextbox(msoTextOrientationHorizontal, 25.55*72, 14.21*72, 1.12*72, 20)
oshp.name = "SlideNum"
Else
Set oshp = oS
End If
With oshp.TextFrame.TextRange
' .Text = CStr(oSl.SlideIndex) & "/" & CStr(ActivePresentation.Slides.Count)
.Text = CStr(oSl.SlideIndex)
.Font.Size = 40
End With
Next
结束子