Delete Method

Microsoft Word Visual Basic

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

ShowDelete 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.

ShowDelete 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.

ShowDelete 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

ShowAs it applies to the Cell object.

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

Sub DeleteCells()
    Dim intResponse As Integer

    intResponse = MsgBox("Are you sure you want " & _
        "to delete the cells?", vbYesNo)
    
    If intResponse = vbYes Then
        ActiveDocument.Tables(1).Cell(1, 1).Delete
    End If
End Sub

ShowAs it applies to the Range and Selection objects.

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

Sub DeleteSelection()
    Dim intResponse As Integer
    
    intResponse = MsgBox("Are you sure you want to " & _
        "delete the contents of the document?", vbYesNo)
    
    If intResponse = vbYes Then
        ActiveDocument.Content.Select
        Selection.Delete
    End If
End Sub

ShowAs it applies to the Bookmark object.

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

Sub DeleteBookmark()
    Dim intResponse As Integer
    Dim strBookmark As String
    
    strBookmark = "temp"
    
    intResponse = MsgBox("Are you sure you want to delete " _
        & "the bookmark named """ & strBookmark & """?", vbYesNo)
    
    If intResponse = vbYes Then
        If ActiveDocument.Bookmarks.Exists(Name:=strBookmark) Then
            ActiveDocument.Bookmarks(Index:=strBookmark).Delete
        End If
    End If
End Sub