在word中的图形对象有InlineShape对象和Shape对象,其中InlineShape对象表示的是嵌入到文本层的图片,就像是文字的一部分一样,而Shape对象是悬浮于文本之上位于图形层,两者有区别。
如果需要将所有除了文字以外的对象都清除,需要同时遍历InlineShape对象和Shape对象。
基于以上的认识,可以使用如下的代码删除word文档中的所有图形对象:
Sub QQ1722187970()
Dim oDoc As Document
Set oDoc = Word.ActiveDocument
Dim oSP As Shape
Dim oInLineSp As InlineShape
With oDoc
For Each oSP In .Shapes
oSP.Delete
Next
For Each oInLineSp In .InlineShapes
oInLineSp.Delete
Next
End With
End Sub
以上代码只能删除word文档的正文中的所有图形对象,如果要删除页眉、页脚中的所有图形对象,还可以使用如下的代码:
Sub QQ1722187970()
Dim oDoc As Document
Set oDoc = Word.ActiveDocument
Dim oSec As Section
Dim oShape As Shape
Dim oInLineShape As InlineShape
With oDoc
'先遍历所有的节对象
For Each oSec In .Sections
With oSec
With .Headers(wdHeaderFooterPrimary)
For Each oShape In .Shapes
oShape.Delete
Next
For Each oInLineShape In .Range.InlineShapes
oInLineShape.Delete
Next
End With
With .Footers(wdHeaderFooterPrimary)
For Each oShape In .Shapes
oShape.Delete
Next
For Each oInLineShape In .Range.InlineShapes
oInLineShape.Delete
Next
End With
End With
Next
End With
End Sub


发表评论