OfficeTips Home || VBA Section || General Section || Download Section || Privacy Policy

Enumerate Hyperlinks and the associated objects.

It's possible to enumerate the hyperlinks in a presentation quickly. However a little known fact is that it is also possible to ascertain to which shape/text range a particular hyperlink is associated with by clever use of the Parent object.

More about the Parent property: Returns the reference to the parent of any object.

e.g
Set oSld = ActivePresentation.Slides(1) and Set oSld = ActivePresentation.Slides(1).Shapes(1).Parent are equivalent because the return the same slide reference. In the 2nd statement we retrieve the reference of the parent of the given shape object which is the slide itself. You might wonder how this is useful. In the example for better explanation I traversed an addition level down and then came back up to arrive at the slide reference which is not good programming. But what if you were given a shape reference and needed to ascertain which was the parent presentation within a sub routine, would this property be very handy then? Using this property we can traverse in reverse to arrive at any top level object associated with the original object.

If you wanted to know if a shape resides on a slide or master.

If TypeName(ActiveWindow.Selection.ShapeRange(1).Parent) = "Master" Then
    Debug.Print "Shape is on a master slide"
Else
    Debug.Print "Shape is on a slide"
End If

 

If you want to get a reference to the top level presentation from a shape reference assuming oShp is a shape object.

Set oPres = oShp.Parent.Parent 'Shape->Slide->Presentation
Let's use this property to arrive at what we initially set out.


Sub EnumHyperlinksParent()
Dim oSld As Slide
Dim I As Integer
'Parent could be a shape or textrange hence we use object declaration
Dim oParent As Object
For Each oSld In ActivePresentation.Slides
    Debug.Print "Slide number: " & oSld.SlideNumber
    Debug.Print "Hyperlink count: " & oSld.Hyperlinks.Count
    For I = 1 To oSld.Hyperlinks.Count
        'The parent object for a hyperlink is ActionSetting
        'The parent object for an ActionSetting is the PPT shape/textrange
        'By querying the type of oParent object, you can ascertain
        'if it is associated with shape or text.

        Set oParent = oSld.Hyperlinks(I).Parent.Parent
        'Hyperlink->ActionSetting->Shape/TextRange

        Debug.Print CStr(I) & " Hyperlink: " & oSld.Hyperlinks(I).Address & _
                oSld.Hyperlinks(I).SubAddress
        If TypeName(oParent) = "TextRange" Then
            Debug.Print " Associated TextRange: " & oParent & _
            ", of shape: " & oParent.Parent.Parent.Name 'TextRange->TextFrame->Shape
        Else
            Debug.Print " Associated Shape : " & oParent.Name
        End If
    Next I
    Debug.Print 'Blank line
Next oSld
' To make this example complete you need to enumerate the slide masters too but that
' is left as exercise.

End Sub

Update: Bug in PPT 2007 - If there is a hyperlink present on a text range within a table then oParent.Parent.Parent.Name will fail.

 


Copyright 1999-2018 (c) Shyam Pillai. All rights reserved.