在用vba处理word表格时,经常会遇到需要遍历word单元格的内容。
比如如下所示的word表格中

需要定位每个单元格,并删除单元格内容中的前4个字符,这时候可以使用如下的vba代码:
Sub QQ1722187970()
Dim oTable As Table
Dim oCell As Cell
Dim oDoc As Document
Set oDoc = Word.ActiveDocument
For Each oTable In oDoc.Tables
With oTable
For Each oCell In .Range.Cells
oCell.Select
Word.Selection.Collapse
Word.Selection.Delete wdCharacter, 4
Next
End With
Next
End Sub
此类问题的关键点是要用Selection对象进行操作。


发表评论