当前位置: 动力学知识库 > 问答 > 编程问答 >

View slide title in Powerpoint VBA editor

问题描述:

In the Powerpoint VBA editor, we get a list of slides in the VBA project window, categorised under "Microsoft PowerPoint Objects". This list will include slides that have ActiveX controls on them.

The slides appear with numbers on them ("Slide1", "Slide3", etc), and these numbers look like they are based on the order in which the slides were added to the presentation - not the actual order of the slides in the current presentation. However, the title or name of the slides is not included. This makes it confusing to work with, and difficult to find the slide that has the control one wants to work with.

In Excel VBA, the layout of the editor is the same, with a list of worksheets. However, in Excel the name of the sheet is shown in brackets after the sheet number. So if Sheet1 is called "MyDataSheet", it will show up as "Sheet1 (MyDataSheet)".

How can I achieve something similar in Powerpoint? Is there are way to control the name/title that is used to display each slide in the Powerpoint editor?

网友答案:

This is one of the oddities of the PPT object model. Each slide has an internal name that's assigned by PPT at the time the slide is created. That's what you see in the IDE. Each slide also has a .Name property that's initially the same as the slide name assigned by PPT, but that you can change programmatically.

PPT still shows the assigned name under "Microsoft PowerPoint Objects" but if you look at the Properties window of the IDE (press F4 to display it) you'll see (and can edit) the slide's .Name property. This would let you know which slide you've selected in the IDE.

You could also run a bit of code to change the .Name property in a way that reflect's the slide order:

Sub Thing()
    Dim oSl As Slide
    For Each oSl In ActivePresentation.Slides
        oSl.Name = "SLIDE_" & CStr(oSl.SlideIndex)
    Next
End Sub

If you want to get a bit fancier, you can have it pick up the slide's title (if any) as the .Name and use SLIDE_index as the .Name if not:

Sub Thing()
    Dim oSl As Slide
    Dim sTemp As String

    For Each oSl In ActivePresentation.Slides
        sTemp = SlideTitle(oSl)
        If Len(sTemp) > 0 Then
            oSl.Name = oSl.Shapes.Title.TextFrame.TextRange.Text
        Else
            oSl.Name = "SLIDE_" & CStr(oSl.SlideIndex)
        End If
    Next
End Sub

Function SlideTitle(oSl As Slide) As String

    On Error GoTo ErrHandler
    SlideTitle = oSl.Shapes.Title.TextFrame.TextRange.Text

NormalExit:
    Exit Function
ErrHandler:
    SlideTitle = ""
    GoTo NormalExit

End Function

One thing to be careful of here is that PPT may or may not complain if you try to give two slides the same .Name property. In other words, you may want to make sure that your slide titles are unique. There might be other issues (characters that aren't allowed in .Name for example).

分享给朋友:
您可能感兴趣的文章:
随机阅读: