How to delete cell contents in Word with VBA?
Asked Answered
S

4

10

I've looked at the documentation for table cell objects and selection objects in VBA, and I didn't see any way to delete cell contents in Word while retaining the cell itself. It looks like doing so is easy in Excel, and next to impossible in Word.

Some cells I need to do this for will contain text, others will contain text form fields. Any ideas?

Savoury answered 4/5, 2011 at 3:49 Comment(0)
D
15

This works:

ActiveDocument.Tables(1).Cell(1, 2).Select
Selection.Delete

This deletes the cell contents but leaves the empty cell behind.

I understand your dismay, because oddly, the above does not do the same as

ActiveDocument.Tables(1).Cell(1, 2).Delete

which deletes the entire cell!

The former is the equivalent of selecting a cell and pressing the Delete key (which clears the contents but leaves the cell in place). The latter is the equivalent of right-clicking a cell and choosing "Delete cells..." (which deletes the cell).

Druggist answered 4/5, 2011 at 7:14 Comment(0)
B
3

Sorry for digging up such an old question, but hopefully someone will find this useful. If you prefer to avoid the Select method, the following is what you're looking for:

ActiveDocument.Tables(1).Cell(1, 1).Range.Text = ""

It deletes images and content controls as well.

Bypass answered 19/8, 2020 at 8:19 Comment(0)
S
1

I cobbled this together from various parts of the interwebs... including Fumei from VBA Express. It's working well. Select any cells in your table and run the macro deleteTableCells

Sub deleteTableCells()
Dim selectedRange As Range

On Error GoTo Errorhandler
Set selectedRange = SelectionInfo

selectedRange.Delete

Errorhandler:
Exit Sub
End Sub

Function SelectionInfo() As Range
     '
    Dim iSelectionRowEnd As Integer
    Dim iSelectionRowStart As Integer
    Dim iSelectionColumnEnd As Integer
    Dim iSelectionColumnStart As Integer
    Dim lngStart As Long
    Dim lngEnd As Long
     
     ' Check if Selection IS in a table
     ' if not, exit Sub after message
    If Selection.Information(wdWithInTable) = False Then
        Err.Raise (2022)
    Else
        lngStart = Selection.Range.Start
        lngEnd = Selection.Range.End
         
         ' get the numbers for the END of the selection range
        iSelectionRowEnd = Selection.Information(wdEndOfRangeRowNumber)
        iSelectionColumnEnd = Selection.Information(wdEndOfRangeColumnNumber)
         
         ' collapse the selection range
        Selection.Collapse Direction:=wdCollapseStart
         
         ' get the numbers for the END of the selection range
         ' now of course the START of the previous selection
        iSelectionRowStart = Selection.Information(wdEndOfRangeRowNumber)
        iSelectionColumnStart = Selection.Information(wdEndOfRangeColumnNumber)
         
         ' RESELECT the same range
        Selection.MoveEnd Unit:=wdCharacter, Count:=lngEnd - lngStart
         
         ' set the range of cells for consumption
        With ActiveDocument
            Set SelectionInfo = .Range(Start:=.Tables(1).cell(iSelectionRowStart, iSelectionColumnStart).Range.Start, _
                            End:=.Tables(1).cell(iSelectionRowEnd, iSelectionColumnEnd).Range.End)
        
        End With
    End If
End Function
Smuggle answered 23/6, 2022 at 5:50 Comment(0)
C
0
Private Sub cbClearTable_Click()  
'mouse cursor must be in the table for clearing  
Dim cell_BhBp As Cell  
For Each cell_BhBp In Selection.Tables(1).Range.Cells  
cell_BhBp.Range = ""  
Next  
End Sub

The code above clears the contents in all cells in the current table /the table, where the mouse cursor is/

One other way to clear all table cells of first table in document is

ActiveDocument.Tables(1).Range.Delete

Or for current table /where the cursor is in/

Selection.Tables(1).Range.Delete

    Private Sub CommandButton40_Click()
    Application.Activate
    SendKeys ("{DEL}")
    End Sub

The code above clears contents of all selected cells. In this case, the selected cells may not be adjacent. This code is fired when button of user form is clicked.

Colonist answered 16/8, 2022 at 21:33 Comment(1)
While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.Cipango

© 2022 - 2024 — McMap. All rights reserved.