如何用VBA删除Word中的单元格内容?
我查看了 VBA 中表格单元格对象和选择对象的文档,但没有看到任何方法可以在保留单元格本身的同时删除 Word 中的单元格内容。看起来在 Excel 中这样做很容易,但在 Word 中几乎不可能。
我需要执行此操作的某些单元格将包含文本,其他单元格将包含文本表单字段。有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是有效的:
这会删除单元格内容,但留下空单元格。
操作不一样
我理解您的沮丧,因为奇怪的是,上面的操作与删除整个单元格的
!前者相当于选择一个单元格并按Delete键(这会清除内容但将单元格保留在原位)。后者相当于右键单击单元格并选择“删除单元格...”(这将删除单元格)。
This works:
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
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).
很抱歉挖掘出这样一个老问题,但希望有人会发现这很有用。如果您希望避免使用
Select
方法,那么您需要的是以下内容:它还会删除图像和内容控件。
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:It deletes images and content controls as well.
我从互联网的各个部分拼凑了这个......包括 VBA Express 的 Fumei。运作良好。选择表格中的任何单元格并运行宏 deleteTableCells
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
上面的代码清除当前表格/鼠标光标所在的表格中的所有单元格中的内容
另一种清除文档中第一个表格的所有表格单元格的方法是
或者对于当前表格/光标所在的位置/
代码上面清除了所有选定单元格的内容。在这种情况下,所选单元格可能不相邻。单击用户表单的按钮时会触发此代码。
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
Or for current table /where the cursor is in/
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.