How to delete cell contents in Word with VBA?

jeremiahs picture jeremiahs · May 4, 2011 · Viewed 14.4k times · Source

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?

Answer

Jean-François Corbett picture Jean-François Corbett · May 4, 2011

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