VBA Add Multiple Layers

Discussion created by DuncanRager on Apr 26, 2011
Latest reply on Apr 27, 2011 by Hornbydd

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
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?