Delete Method (ActiveX)

Deletes a specified object or a set of saved layer settings.

Supported platforms: Windows only

Signature

VBA:

object.Delete
object

Type: All drawing objects, AttributeReference, Block, Dictionary, DimStyle, Group, Hyperlink, Layer, LayerStateManager, Layout, Linetype, Material, MLeaderStyle, PlotConfiguration, PopupMenuItem, RegisteredApplication, SelectionSet, TableStyle, TextStyle, Toolbar, ToolbarItem, UCS, View, Viewport, XRecord

The objects this method applies to.

Return Value (RetVal)

No return value.

Remarks

When you delete an object in a collection, all remaining items in the collection are reassigned a new index based on the current count. You should therefore avoid loops that delete an object while iterating through the collection. For example, the following VBA code will result in a runtime error:

For i = 0 To ThisDrawing.Groups.Count - 1
    ThisDrawing.Groups.Item(i).Delete
Next I

Instead, use the following VBA code to delete all members in a collection:

For Each obj In ThisDrawing.Groups
    obj.Delete
Next obj

You can also use the following VBA code to delete a single member of a collection:

ThisDrawing.Groups.Item("group1").Delete

An error will result if you attempt to delete a collection object.

ToolbarItem: You can only add or remove toolbar items when the toolbar is visible.

LayerStateManager: This object takes an argument, Name, which is a string representing the layer state to be deleted.

Examples

VBA:

Sub Example_Delete()
    ' This example creates a Layer named "TEST".
    ' It then iterates the Layers collection and displays
    ' the names of the available layers.
    ' It then deletes the layer "TEST", and again iterates
    ' the layers collection and displays the names of
    ' available layers.
    
    Dim layerObj As AcadLayer
    
    ' Create the new layer
    Set layerObj = ThisDrawing.Layers.Add("TEST")
    
    ' Display the names of the layers in the drawing
    GoSub DISPLAYLAYERS
    
    ' Delete the layer "TEST"
    layerObj.Delete
    
    ' Display the names of the layers remaining in the drawing
    GoSub DISPLAYLAYERS
    
    Exit Sub
    
DISPLAYLAYERS:
    Dim entry As AcadLayer
    Dim layerNames As String
    layerNames = ""
    For Each entry In ThisDrawing.Layers
       layerNames = layerNames & entry.name & ", "
    Next
    MsgBox "The drawing consists of the following layers:" & vbCr & layerNames, , "Delete Example"
    Return
    
End Sub

Visual LISP:

(vl-load-com)
(defun c:Example_Delete()
    ;; This example creates a Layer named "TEST".
    ;; It then iterates the Layers collection and displays
    ;; the names of the available layers.
    ;; It then deletes the layer "TEST", and again iterates
    ;; the layers collection and displays the names of
    ;; available layers.
    (setq acadObj (vlax-get-acad-object))
    (setq doc (vla-get-ActiveDocument acadObj))
    
    ;; Create the new layer
    (setq layerObj (vla-Add (vla-get-Layers doc) "TEST"))
    
    ;; Display the names of the layers in the drawing
    (setq layerNames "")
    (vlax-for entry (vla-get-Layers doc)
       (setq layerNames (strcat layerNames (vla-get-Name entry) ", "))
    )
    (alert (strcat "The drawing consists of the following layers: \n" layerNames))
    
    ;; Delete the layer "TEST"
    (vla-Delete layerObj)
    
    ;; Display the names of the layers remaining in the drawing   
    (setq layerNames "")
    (vlax-for entry (vla-get-Layers doc)
       (setq layerNames (strcat layerNames (vla-get-Name entry) ", "))
    )
    (alert (strcat "The drawing consists of the following layers: \n" layerNames))
)