I'm stuck on how to collapse all layers in my ArcMap projects (9.3). I've tried a few bits of sample code, but they all seem to fail for some reason or other.

Some of the layers might be grouped, and some are imagery or WMS layers if that makes a difference.

Here is some code I have tinkered with, but does not work:

Dim pMxdoc As IMxDocument
Set pMxdoc = ThisDocument
Dim pFeatLayer As IFeatureLayer
Dim pEditLayers As IEditLayers
Set pEditLayers = pEditor
Dim pMap As IMap
Set pMap = pMxdoc.FocusMap
Dim pLayer As ILayer

'Collapse any expanded grouplayers
Dim pGroupLayer As IGroupLayer
For i = 0 To pMxdoc.FocusMap.LayerCount - 1
Set pLayer = pMxdoc.FocusMap.Layer(i)
'Check if the reached layer is a group
If TypeOf pLayer Is IGroupLayer Then
    Set pGroupLayer = pLayer
    if pGroupLayer.Expanded = true Then
        pGroupLayer.Expanded = false
    End if
End If
Next i

enter image description here

  • Have you tried pGroupLayer.Expanded = false? That's the syntax for C#, so just taking a stab at it. Dec 19, 2011 at 19:33
  • Hi Chad, I cleaned up the code as you suggested. It does work for the Grouped layers now, so it's close... What interface or ArcObjects should be used for non-grouped layers? I don't see the Expanded property available with pLayer.
    – User Error
    Dec 19, 2011 at 20:34
  • see my answer below. Dec 19, 2011 at 20:47
  • Thank you. Using the listed methods, I was able to collapse/expand the feature, group, raster and annotation layers. Does anyone who know a method for topology and mosaic dataSet?
    – stef
    Nov 15, 2017 at 15:28
  • Please don't add "thank you" as an answer. Instead, vote up the answers that you find helpful. - From Review
    – gisnside
    Nov 15, 2017 at 16:06

4 Answers 4


Try pGroupLayer.Expanded = false to collapse the group. As far as non-grouped layers, here is a snippet I used to collapse the legends on rasters in the TOC, it uses the ILegendGroup interface:

// Collapse the legends on each raster
ILegendInfo legendInfo = (ILegendInfo)pRasterLayer.Renderer;
ILegendGroup legendGroup = (ILegendGroup)legendInfo.get_LegendGroup(0);
legendGroup.Visible = false;
  • 1
    Note that some layers can have more than one legend group (unique value renderers, etc.) so to be sure, you'll want to iterate over the legend groups and set each one's Visible property to false: forums.esri.com/…
    – blah238
    Dec 19, 2011 at 20:49
  • Thank you both, this is much more complicated than I imagined. Michael Knight's code on the ESRI forum gets as far as a grouped annotation layer in my .mxd then crashes with a Type mismatch error. Not sure what is special about one of the featureclasses in there but the "09 - Stream Labels" one doesn't have a "+" or "-".
    – User Error
    Dec 19, 2011 at 22:05
  • 1
    @UserError - that's odd. Is the layer valid? I would think that it would have to have some sort of symbology associated with it. Maybe test for the presence of the legend group first, and if there isn't one, move on? Dec 19, 2011 at 22:22

Extending on Chad's answer, following does the trick for feature layer with multiple legend groups

for (int k = 0; k <= (featureLayer as ILegendInfo).LegendGroupCount - 1; k++)
   (featureLayer as ILegendInfo).get_LegendGroup(k).Visible = false;

When the layer is an IAnnotationLayer, use the ICompositeLayer2 interface:

ICompositeLayer2 compositeLayer = (ICompositeLayer2)featureLayer;
compositeLayer.Expanded = false;

If you want to expand or collapse all layers, select the layers and either hit the subtract key or the plus key on the keyboard. Also, if you want to check or uncheck them, hit the spacebar on the selected layers.

  • This works in ArcMap GUI, but question is related with VBA syntax.
    – aldo_tapia
    Dec 5, 2016 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.