在所选幻灯片的标题中添加或替换幻灯片编号

时间:2019-10-30 12:28:19

标签: vba powerpoint

我想创建一个宏,该宏将以(1/5)格式添加到所选幻灯片标题的末尾。

我设法通过添加编号来写该部分。我无法为查找和替换现有编号准备vba。无论出于何种原因,都需要更改幻灯片顺序并需要对其进行更新。

Sub SlideNumbering()

Dim shp As shape
Dim sld As Slide
Dim SldAll As Single
Dim SldNr As Single

SldAll = Application.ActiveWindow.Selection.SlideRange.Count

SldNr = SldAll

For s = SldAll To 1 Step -1
    ActivePresentation.Slides(s).Shapes.Title.TextFrame.TextRange.InsertAfter " (" & SldNr & "/" & SldAll & ")"
    SldNr = SldNr - 1
Next

End Sub

1 个答案:

答案 0 :(得分:0)

这是一个删除现有编号的宏。这使用正则表达式模式查找空格,方括号,任意数字,反斜杠,任意数字和右方括号的序列:

Sub DeleteNumbering()
  Dim regX As Object
  Dim oSlide As Slide
  Dim oShape As Shape
  Dim Foundb As Boolean
  Dim NewText$

  Set regX = CreateObject("vbscript.regexp")
  With regX
    .Global = True
    .Pattern = " \(\d(/)\d\)"
  End With
  ReplaceWord = ""

  For Each oSlide In ActivePresentation.Slides
    For Each oShape In oSlide.Shapes
      If oShape.Type = msoPlaceholder Then
        If (oShape.PlaceholderFormat.Type = ppPlaceholderCenterTitle _
        Or oShape.PlaceholderFormat.Type = ppPlaceholderTitle) _
        And oShape.TextFrame.HasText Then
          Foundb = regX.Test(oShape.TextFrame.TextRange.Text)
          If Foundb = True Then
            NewText$ = regX.Replace(oShape.TextFrame.TextRange.Text, "")
            oShape.TextFrame.TextRange.Text = NewText$
          End If
        End If
      End If
    Next oShape
  Next oSlide
End Sub