Thank you very much Rich. But the problem seems to lie in the index of the legendgroup. For a CAD-Layer the legend is more complex. Trying the following code it now works.
For intLegendIndex = 0 To pLegendInfo.LegendGroupCount - 1
pLegendGroup = pLegendInfo.LegendGroup(intLegendIndex)
pLegendGroup.Visible = False
Next intLegendIndex
Kind regards,
Anika
The whole code looks like this:
Public Sub LeyendasVisibilidadForum()
Try
Dim pMXDoc As IMxDocument
pMXDoc = My.ArcMap.Document
Dim pMap As IMap
pMap = pMXDoc.FocusMap
Dim numLayer As Integer
numLayer = pMap.LayerCount
Dim pLayer As ILayer
Dim pCadLayer As ICadDrawingLayers
Dim pLegendInfo As ILegendInfo
Dim pLegendGroup As ILegendGroup
Dim i As Integer
Dim intLegendIndex As Integer
Dim strPolygon1 As String = "*Polygon*"
Dim strPolygon3 As String = "*Point*"
Dim strPolygon4 As String = "*Polyline*"
Dim strPolygon5 As String = "*MultiPatch*"
Dim strPolygon6 As String = "toponaa1"
For i = 0 To numLayer - 1
pLayer = pMap.Layer(i)
If pLayer.Name Like strPolygon5 Then
pLayer.Visible = False
If Not pLayer Is Nothing And (TypeOf pLayer Is ICadDrawingLayers) Then
pCadLayer = pLayer
pLegendInfo = pCadLayer
For intLegendIndex = 0 To pLegendInfo.LegendGroupCount - 1
pLegendGroup = pLegendInfo.LegendGroup(intLegendIndex)
pLegendGroup.Visible = False
Next intLegendIndex
End If
ElseIf pLayer.Name Like strPolygon1 Then
pLayer.Visible = False
If Not pLayer Is Nothing And (TypeOf pLayer Is ICadDrawingLayers) Then
pCadLayer = pLayer
pLegendInfo = pCadLayer
For intLegendIndex = 0 To pLegendInfo.LegendGroupCount - 1
pLegendGroup = pLegendInfo.LegendGroup(intLegendIndex)
pLegendGroup.Visible = False
Next intLegendIndex
End If
ElseIf pLayer.Name Like strPolygon3 Then
pLayer.Visible = False
If Not pLayer Is Nothing And (TypeOf pLayer Is ICadDrawingLayers) Then
pCadLayer = pLayer
pLegendInfo = pCadLayer
For intLegendIndex = 0 To pLegendInfo.LegendGroupCount - 1
pLegendGroup = pLegendInfo.LegendGroup(intLegendIndex)
pLegendGroup.Visible = False
Next intLegendIndex
End If
ElseIf pLayer.Name Like strPolygon4 Then
If Not pLayer Is Nothing And (TypeOf pLayer Is ICadDrawingLayers) Then
pCadLayer = pLayer
pLegendInfo = pCadLayer
For intLegendIndex = 0 To pLegendInfo.LegendGroupCount - 1
pLegendGroup = pLegendInfo.LegendGroup(intLegendIndex)
pLegendGroup.Visible = False
Next intLegendIndex
End If
ElseIf pLayer.Name Like strPolygon6 Then
pLayer.Visible = False
End If
Next
pMXDoc.UpdateContents()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub