vba 如何使用VBA删除Word中的单元格内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5878294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:09:26  来源:igfitidea点击:

How to delete cell contents in Word with VBA?

vbams-wordtablecell

提问by jeremiahs

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.

我在 VBA 中查看了表格单元格对象和选择对象的文档,我没有看到任何方法可以在保留单元格本身的同时删除 Word 中的单元格内容。看起来在 Excel 中这样做很容易,而在 Word 中几乎不可能。

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

我需要执行此操作的某些单元格将包含文本,其他单元格将包含文本表单字段。有任何想法吗?

回答by Jean-Fran?ois Corbett

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 notdo 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 Deletekey (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).

前者相当于选择一个单元格并按下Delete键(清除内容但保留单元格)。后者相当于右键单击单元格并选择“删除单元格...”(删除单元格)。