How to work with style gallery auto symbol selection


This sample goes through all polygon layers in the map and attempts to match the symbology from the standard style set to the layer name. ArcMap does this by default. Therefore, to see a real difference before testing the tool, layer names should be changed to reflect suitable styles. For example, try changing a layer name to "Glacier" and executing this command.
The code samples in this section show the fundamentals of programming with ArcObjects. A careful reading of them gives you all the important concepts you need for developing with ArcObjects, as well as an introduction to the most important ArcObjects components.
The code can be typed or copied into the VBA environment in ArcMap or ArcCatalog, after which you can follow through with the VBA debugger.

How to use

  1. Add the code to the Click event of a UIButtonControl in ArcMap.
[VBA]
Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument

Dim pStyleItems As IEnumStyleGalleryItem
Set pStyleItems = pMxDoc.StyleGallery.Items("Fill Symbols", "ESRI.style", "Default")
Dim pGalleryItem As IStyleGalleryItem

Dim pRenderer As ISimpleRenderer
Dim pGeoFeatureLayer As IGeoFeatureLayer
Dim i As Long
For i = 0 To pMxDoc.FocusMap.LayerCount - 1
    If (TypeOf pMxDoc.FocusMap.Layer(i) Is IFeatureLayer) Then
        Set pGeoFeatureLayer = pMxDoc.FocusMap.Layer(i)
        If (pGeoFeatureLayer.FeatureClass.ShapeType = esriGeometryPolygon) Then
            pStyleItems.Reset
            Set pGalleryItem = pStyleItems.Next
            Do While (Not pGalleryItem Is Nothing)
                If (pGeoFeatureLayer.Name = pGalleryItem.Name) Then
                    Set pRenderer = pGeoFeatureLayer.Renderer
                    Set pRenderer.Symbol = pGalleryItem.Item
                    Exit Do
                End If
                Set pGalleryItem = pStyleItems.Next
            Loop
        End If
    End If
Next i
pMxDoc.ActivatedView.PartialRefresh esriViewGeography, Nothing, Nothing