ArcMap の TOC で複数の選択マップやレイヤーを取得する方法

Sub GetSelectedItems()
    Dim pMxDocument As IMxDocument
    Set pMxDocument = ThisDocument
    
    Dim pContentsView As IContentsView
    Set pContentsView = pMxDocument.ContentsView(0) '最上位データフレーム
    
    Dim mySet As ISet
    Set mySet = New esriSystem.Set
    
    Dim pVariant As Variant
    Set pVariant = pContentsView.SelectedItem
    
    Dim pUnknown As IUnknown
    Dim pMap As IMap
    Dim pLayer As ILayer
    Dim i As Long
    
    'データフレーム
    If TypeOf pVariant Is IMap Then
        Set pMap = pVariant
        Debug.Print pMap.Name
    
    'レイヤ
    ElseIf TypeOf pVariant Is ILayer Then
        Set pLayer = pVariant
        Debug.Print pLayer.Name
    
    '複数データフレーム/レイヤ
    ElseIf TypeOf pVariant Is ISet Then
        Set mySet = pVariant
        
        For i = 0 To mySet.Count - 1
            
            Set pUnknown = mySet.Next
                               
            If TypeOf pUnknown Is IMap Then
                Set pMap = pUnknown
                Debug.Print pMap.Name
                
            ElseIf TypeOf pUnknown Is ILayer Then
                Set pLayer = pUnknown
                Debug.Print pLayer.Name
                
            End If
        Next
        
    End If
    
End Sub