VBA Add Multiple Layers

482
1
04-26-2011 09:54 AM
DuncanRager
New Contributor
Greetings,

Just learning VBA and ArcObjects, and came up with this simple procedure to add a layer from an Arc Image Server.

Private Sub Streams_and_Waterbodies_Click()

    'create GxLayer object and specify its path
    Dim pGxLayerCls As IGxLayer
    Set pGxLayerCls = New GxLayer
    
    Dim pGxFile As IGxFile
    Set pGxFile = pGxLayerCls
    
    pGxFile.Path = "L:\LayerFiles\Hydrography\Stream.lyr"
    
    'grab current dataframe
    Dim pMxDoc As IMxDocument
    Set pMxDoc = ThisDocument
    
    Dim pMap As IMap
    Set pMap = pMxDoc.FocusMap
    
    'Assign the layer a name and make it visible when added
    Dim pLayer As ILayer
    Set pLayer = pGxLayerCls.Layer
    pLayer.Name = "Streams"
    pLayer.Visible = True
        
    'Add layer to map
    pMap.AddLayer pGxLayerCls.Layer
    
    'Refresh Viewer and update TOC
    pMxDoc.ActiveView.Refresh
    pMxDoc.UpdateContents
    
End Sub


So now, I want to extend this script to add the "Waterbodies" layer also, because at this moment only the "Streams" layer is being added.

Do I have to re-initiate each class object for the new path? Or is there a simpler/shorter way of adding the second path name without doubling up the size of the script?


Thanks,

DR
0 Kudos
1 Reply
DuncanHornby
MVP Notable Contributor
Duncan,

I see you are loading layer files, you are better off using the ILayerFile interface in my opinion. It would certainly simplify your code and that is always a good thing if you are learning code.

Also you are wasting your time learning VBA as ESRI have decided to bin it in favour of Python, so you should make the effort to learn that instead.

Duncan
0 Kudos