PPT根据节自动生成目录

alt+F11,插入模块,粘贴代码

Sub InsertSectionTOCAtCursor()
    Dim pptPres As Presentation
    Dim tocText As String
    Dim sectionIndex As Integer
    Dim sectionName As String
    Dim firstSlideIndex As Integer
    Dim slideNumber As Integer
    Dim currentTextRange As textRange
    
    ' 获取当前演示文稿
    Set pptPres = Application.ActivePresentation
    
    ' 初始化目录文本
    tocText = "" ' 不再需要“目录”和空行
    
    ' 遍历所有节
    For sectionIndex = 1 To pptPres.SectionProperties.Count
        ' 获取节的名称
        sectionName = pptPres.SectionProperties.Name(sectionIndex)
        
        ' 获取该节的第一个幻灯片的索引
        firstSlideIndex = pptPres.SectionProperties.FirstSlide(sectionIndex)
        
        ' 获取该节第一个幻灯片的页码
        slideNumber = pptPres.Slides(firstSlideIndex).slideNumber
        
        ' 将节名和页码添加到目录文本中,格式为“节名 页码”
        ' tocText = tocText & sectionName & " " & slideNumber & vbCrLf
        tocText = tocText & sectionName & vbCrLf
    Next sectionIndex
    
    ' 去掉最后一个vbCrLf
    If Len(tocText) > 0 Then
        tocText = Left(tocText, Len(tocText) - Len(vbCrLf))
    End If
    
    
    ' 检查当前是否有选中的文本框
    On Error Resume Next
    Set currentTextRange = Application.ActiveWindow.Selection.textRange
    On Error GoTo 0
    
    ' 如果当前有选中的文本框,则在光标处插入目录文本
    If Not currentTextRange Is Nothing Then
        currentTextRange.InsertAfter tocText
        
         ' 应用双字节圆圈编号格式
        With currentTextRange.ParagraphFormat.Bullet
            .Visible = True
            .Type = ppBulletNumbered
            .Style = ppBulletCircleNumDBPlain
        
        End With
        
    Else
    	' 如果没有进入光标编辑模式,弹出提示框
        MsgBox "请先进入光标编辑模式", vbExclamation, "提示"
    End If
End Sub

操作:选中文本框,进入光标编辑模式,alt+f8,运行宏。
要求:每节下至少有一张幻灯片

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值