Search code examples
vbapowerpoint

How to have the list of links from PowerPoint in a dropdown list


I need to have a list of all links in PowerPoint in a dropdown list in a form.

To find the links I have already the following code:

Sub listing()
    Dim oSld As Slide
    Dim oSh As Shape
        For Each oSld In ActivePresentation.Slides
        For Each oSh In oSld.Shapes
    
       If oSh.Type = msoLinkedPicture Or oSh.Type = msoLinkedOLEObject Then
           Path = oSh.LinkFormat.SourceFullName
           Position = InStr(1, Path, "!", vbTextCompare)
           FileName = Left(Path, Position - 1)
           
           MsgBox FileName ' This will lists each link one by one
       End If
   Next oSh
Next oSld
End Sub

The code above will also produce duplicates as multiple objects have the same link but different ranges in Excel. For example, to the list I need should contain only unique values

To fill in the dropdown in the form I have this code:

Private Sub UserForm_Initialize()

    If ComboBox2.ListCount = 0 Then
        AddDropDownItems
    End If
End Sub

Sub AddDropDownItems()
    ComboBox2.AddItem "1"
    ComboBox2.AddItem "2"
    ComboBox2.AddItem "3"
End Sub

What I can not figure out is how to merge both codes so that it lists the links in the dropdown.


Solution

  • Try this code:

    In the UserForm1 module

    Private Sub UserForm_Initialize()
        Dim oSld As Slide, oSh As Shape
        
        Dim list As New Collection  'declare and make new Collection
        
        For Each oSld In ActivePresentation.Slides
            For Each oSh In oSld.Shapes
                If oSh.Type = msoLinkedPicture Or oSh.Type = msoLinkedOLEObject Then
                    Path = oSh.LinkFormat.SourceFullName
                    Position = InStr(1, Path, "!", vbTextCompare)
                    If Position > 0 Then Path = Left(Path, Position - 1)
                    
                    ' adding paths to the collection to avoid
                    ' duplication (duplicate keys are not allowed)
                    ' also you can use Dictionary object with .Exist() method
                    On Error Resume Next
                    list.Add Path, Path ' add Path as an item and as a key
                    On Error GoTo 0
                End If
            Next oSh
        Next oSld
        
        With Me.ComboBox2
            .Clear
            For Each el In list
                .AddItem el 'add Paths from Collection
            Next
        End With
    End Sub
    

    In the standard module

    Sub test()
        UserForm1.Show
    End Sub