Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete cell contents in Word with VBA?

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?

like image 815
jeremiahs Avatar asked May 04 '11 03:05

jeremiahs


2 Answers

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

like image 152
Jean-François Corbett Avatar answered Nov 13 '22 14:11

Jean-François Corbett


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

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

Deletes images and content controls as well.

like image 24
kb2136 Avatar answered Nov 13 '22 15:11

kb2136