0
votes

I am very new to VBA, and I have a ppt presentation, where I need to get all the shapes from the slides. I need to get the position (left, top) and the size (width, height) also the slide it's linked to. Any help on where should I check first?

I got something, but still need to improve. Don't really know if it is working well.

Sub Slide()

Dim sld As Slide
Dim shp As Shape
Dim count As Integer

count = 1

    For Each sld In ActivePresentation.Slides
        For Each shp In sld.Shapes

            Debug.Print count
            Debug.Print shp.Left
            Debug.Print shp.Top
            count = count + 1

        Next shp
    Next sld

End Sub

1
What did you try so far? Did you check anything? - trincot
Yes, I'll update my question. - Florin Pop

1 Answers

5
votes

A nested loop, looping all the slides in the active presentation and all the shapes in each slide, outputting the information you mentioned plus the ID and Name of each shape. If there is an internal hyperlink, some information about the linked slide is outputted. I also added code to obtain a reference to the object of the linked slide.

Option Explicit

Sub OutputSlides()
    Dim oSlide As Slide
    Dim oShape As Shape
    Dim i As Long
    Dim oAction As ActionSetting
    Dim oHyperlink As Hyperlink

    For Each oSlide In ActivePresentation.Slides
        For Each oShape In oSlide.Shapes
            Debug.Print "Shape #" & oShape.Id & " (" & oShape.Name & ") - Slide: " & oSlide.SlideNumber & " Position: " & oShape.Left & "," & oShape.Top _
                 ; " Size: " & oShape.Width & "x" & oShape.Height


            For Each oAction In oShape.ActionSettings
                On Error Resume Next

                If oAction.Action = ppActionHyperlink Then
                    Set oHyperlink = oAction.Hyperlink

                    ''See more: http://www.pptfaq.com/FAQ00162_Hyperlink_-SubAddress_-_How_to_interpret_it.htm
                    Dim parts() As String
                    Dim slideId As Long
                    Dim slideIndex As Long
                    Dim slideTitle As String
                    Dim linkedSlide As Slide

                    parts = Split(oHyperlink.SubAddress, ",")

                    slideId = CLng(parts(0))
                    slideIndex = CLng(parts(1))
                    slideTitle = parts(2)

                    If slideId > 0 Then
                        Debug.Print "  --Internal hyperlink to slide #: " & slideIndex & "(id: " & slideId&; ", title: " & slideTitle & ")"

                        ''this gets you a reference to the linked slide if you need it:
                        ''Set linkedSlide = oShape.Parent.Parent.Slides(slideIndex)
                    End If

                End If
            Next oAction
        Next oShape
    Next oSlide
End Sub

Example output:

Shape #2 (Title 1) - Slide: 1 Position: 120,88,37504 Size: 720x188
Shape #3 (Subtitle 2) - Slide: 1 Position: 120,283,625 Size: 720x130,375
Shape #4 (CommandButton1) - Slide: 1 Position: 120,73 Size: 237x141
Shape #5 (TextBox1) - Slide: 1 Position: 514,1251,134,875 Size: 72x72
Shape #2 (Title 1) - Slide: 2 Position: 120,88,37504 Size: 720x188
Shape #3 (Subtitle 2) - Slide: 2 Position: 120,283,625 Size: 720x130,375
Shape #4 (CommandButton1) - Slide: 2 Position: 120,73 Size: 237x141
Shape #5 (TextBox1) - Slide: 2 Position: 514,1251,134,875 Size: 72x72
Shape #2 (Title 1) - Slide: 3 Position: 66,28,75 Size: 828x104,375
Shape #3 (Content Placeholder 2) - Slide: 3 Position: 66,143,75 Size: 828x342,625
Shape #5 (Straight Arrow Connector 4) - Slide: 3 Position: 175,4366,201,8028 Size: 263,662x140,9577
--Internal hyperlink to slide #: 3(id: 257, title: Slide 3)