yosie212

move layer up and down doesn't work

Discussion created by yosie212 on Oct 16, 2010
I???m writing code with vba to build tool layer order (move layer up and down), switch from data view to layout view conversely, and turn on and off layer. I have 2 layers named river and lake.  What I want is in data view: the position of layer (0) is river and lake is invisible. And in layout view: the position of layer (0) is lake and river is invisible. The result is layer only move one time every after opening file mxd, whatever the selected layer is, the 2nd run and so on only switching and turning on or off layer. And surprisingly, although the layer lake is invisible but I can see in layout view.
Below are my code and no error. Could anyone please tell me where the mistake is?
Thanks
Yosie
Private Sub MoveLayerDown_Click()
Dim pDoc As IMxDocument
Set pDoc = ThisDocument
Dim pSelLayer As ILayer
Set pSelLayer = pDoc.SelectedLayer
Dim pMap As IMap
Set pMap = pDoc.FocusMap

Dim i As Long
Dim layerIndex As Long

For i = 0 To pMap.LayerCount - 1
    If pMap.Layer(i).Name = pSelLayer.Name Then
        layerIndex = i
    End If
Next i

pMap.MoveLayer pSelLayer, layerIndex + 1

Call View
Call OnOff

End Sub

Private Sub MoveLayerUp_Click()
Dim pDoc As IMxDocument
Set pDoc = ThisDocument
Dim pSelLayer As ILayer
Set pSelLayer = pDoc.SelectedLayer
Dim pMap As IMap
Set pMap = pDoc.FocusMap

Dim i As Long
Dim layerIndex As Long

For i = 0 To pMap.LayerCount - 1
    If pMap.Layer(i).Name = pSelLayer.Name Then
        layerIndex = i
    End If
Next i

pMap.MoveLayer pSelLayer, layerIndex - 1

Call View
Call OnOff

End Sub

Private Sub View()
Dim pDoc As IMxDocument
Set pDoc = ThisDocument
Dim pMap As IMap
Set pMap = pDoc.FocusMap
Dim pLayer As ILayer

If pMap.Layer(0).Name = "Soil" Then
    Set pDoc.activeView = pDoc.FocusMap
Else
    Set pDoc.activeView = pDoc.pageLayout
End If

End Sub

Private Sub OnOff()
Dim pDoc As IMxDocument
Set pDoc = ThisDocument
Dim pMap As IMap
Set pMap = pDoc.FocusMap

pMap.Layer(0).Visible = True
pMap.Layer(1).Visible = False

End Sub

Outcomes