RemoveLayerByID Method

Removes the layer with the specified ID (ID property) from the layer collection of the document. Returns the number of layers, remaining in the collection after the operation.

Applies to: Document object

Syntax

[[Let] countRet =] object.RemoveLayerByID ( layerID )

The RemoveLayerByID method syntax has these parts:

Part Description
object Required. An expression, that returns an instance of the Document object.
layerID

Required. An expression that returns a Long value. Indicates the ID of the layer in the layer to be removed.

countRet Optional. A Long type variable.

Remarks

If the layer with the specified ID (the ID property) wasn't found, the RemoveLayerByID method doesn't remove anything and returns the number of layers in the document. When a layer is removed, the remaining layers are re-indexed - that is, the index of every layer after the removed one is decreased by 1.

You can't remove all layers - at least one layer must exist in the document. An attempt to delete the last layer will have no effect.

Example

This example contains a document-level script. The program removes all layers that don't have shapes on them from the layer collection of the document. First it determines the IDs of all layers and records them into the layerIDs array. Then the IDs of the layers which have shapes on them are erased from the array. The layers with remaining IDs are removed by using the RemoveLayerByID method. The number of removed layers is calculated and displayed on the screen.

' Declare variables
Dim ppage As Page         ' Page
Dim player As Layer       ' Layer
Dim layers_num As Integer ' Number of layers
Dim layerIDs() As Long    ' Layer IDs array

' Get the number of layers
layers_num = thisDoc.LayersNum()
' Create an array to store the IDs of the layers in the document
ReDim layerIDs(layers_num)

' Fill the layerIDs array with IDs of all layers of the document
For i=1 To layers_num
    Let layerIDs(i) = thisDoc.Layer(i).ID
Next i

' Loop through all pages of the document
For i=1 To thisDoc.PagesNum()

    ' Get next page
    Set ppage = thisDoc.Page(i)

    ' Loop through all shapes on the page
    For j=1 To ppage.ShapesNum()
        
        ' For each shape determine the ID of the layer on which it's located
        ' and if it matches the currently used layer, 
        ' erase it
        For k=1 To layers_num
            If layerIDs(k) = ppage.Shape(j).Layer Then
                layerIDs(k) = 0
            End If
        Next k
    Next j
Next i

layers_num = 0
' Loop through all remaining layer IDs 
' and remove corresponding layers
For i=1 To thisDoc.LayersNum()
    If layerIDs(i) <> 0 Then
        thisDoc.RemoveLayerByID( layerIDs(i) )
        layers_num = layers_num + 1
    End If
Next i

' Display the number of removed layers
TRACE "Number of deleted layers = " & layers_num

 

See Also

Layer method, LayerByID method, LayerByName method, LayersNum method, RemoveLayer method, Layer object