Delete Method

Microsoft Word Visual Basic

Show All

Delete Method

       

Delete method as it applies to the Cell and Cells objects.

Deletes a table cell or cells and optionally controls how the remaining cells are shifted.

expression.Delete(ShiftCells)

expression   Required. An expression that returns one of the above objects.

ShiftCells  Optional Variant. The direction in which the remaining cells are to be shifted. Can be any WdDeleteCells constant. If omitted, cells to the right of the last deleted cell are shifted left.

WdDeleteCells can be one of these WdDeleteCells constants.
wdDeleteCellsEntireColumn
wdDeleteCellsEntireRow
wdDeleteCellsShiftLeft
wdDeleteCellsShiftUp

Delete method as it applies to the Range and Selection objects.

Deletes the specified number of characters or words. This method returns a Long value that indicates the number of items deleted, or it returns 0 (zero) if the deletion was unsuccessful.

expression.Delete(Unit, Count)

expression   Required. An expression that returns one of the above objects.

Unit  Optional Variant. The unit by which the collapsed range or selection is to be deleted. Can be one of the following WdUnits constants: wdCharacter (default) or wdWord.

Count  Optional Variant. The number of units to be deleted. To delete units after the range or selection, collapse the range or selection and use a positive number. To delete units before the range or selection, collapse the range or selection and use a negative number.

Delete method as it applies to the ShapeNodes object.

Deletes the specified object.

expression.Delete(Index)

expression   Required. An expression that returns a ShapeNodes object.

Index  Required Long. The number of the shape node to delete.

Delete method as it applies to all other objects in the Applies To list. 

Deletes the specified object.

expression.Delete

expression   Required. An expression that returns one of the objects in the Applies To list.

Example

As it applies to the Cell object.

This example deletes the first cell in the first table of the active document.

Sub DeleteCells()
    ActiveDocument.Tables(1).Cell(1, 1).Delete
End Sub

As it applies to the Range and Selection objects.

This example selects and deletes the contents of the active document.

Sub DeleteSelection()
    ActiveDocument.Content.Select
    Selection.Delete
End Sub

This example collapses the selection and deletes the two words following the insertion point.

Sub DeleteSelection2()
    ActiveDocument.Range(Start:=ActiveDocument.Paragraphs(3).Range.Start, End:=ActiveDocument.Paragraphs(6).Range.End).Select
    Selection.Collapse Direction:=wdCollapseStart
    Selection.Delete Unit:=wdWord, Count:=2
End Sub

This example collapses myRange and deletes the two characters preceding the insertion point.

Sub DeleteRange()
    Dim myRange As Range
    Set myRange = Selection.Words(1)
    myRange.Collapse Direction:=wdCollapseStart
    myRange.Delete Unit:=wdCharacter, Count:=-2
End Sub

This example deletes the first word in the active document.

Sub DeleteFirstWord()
    ActiveDocument.Words(1).Delete
End Sub

As it applies to other objects in the Applies To list. 

If a bookmark named "temp" exists in the active document, this example deletes the bookmark.

Sub DeleteBookmark()
    If ActiveDocument.Bookmarks.Exists(Name:="temp") Then
        ActiveDocument.Bookmarks(Name:="temp").Delete
    End If
End Sub

This example deletes the style named "Intro" from Sales.doc. Paragraphs using the Intro style will revert to using the Normal style.

Sub DeleteStyle()
    Documents(Index:="Sales.doc").Styles _
        (Index:="Intro").Delete
End Sub