我想编写一个宏,将我的word文档中的所有形状和图片转换为内联文本。我正在使用的代码将所有形状(由绘图工具编辑)转换为内联文本,但先前转换为图片(由图片工具编辑)或任何其他图片的表格不是将其包装为内联文本的文本. 粘贴我正在使用的代码
For Each oShp In ActiveDocument.Shapes
oShp.Select
Selection.ShapeRange.WrapFormat.Type = wdWrapInline
Next oShp
第一个答案是一个好的开始,但有一个问题。For Each 迭代集合,但集合由 ConvertToInlineShape 更改。在其他语言中,这会引发集合已被修改的异常,在这里它只是静默停止。
为避免这种情况,您需要将每个形状添加到另一个集合并在那里对其进行迭代。或者在以下示例的情况下,只需手动跟踪索引。
Sub InlineAllImages()
Dim Shape As Shape
Dim Index As Integer: Index = 1
' Store the count as it will change each time.
Dim NumberOfShapes As Integer: NumberOfShapes = Shapes.Count
' Break out if either all shapes have been checked or there are none left.
Do While Shapes.Count > 0 And Index < NumberOfShapes + 1
With Shapes(Index)
If .Type = msoPicture Then
' If the shape is a picture convert it to inline.
' It will be removed from the collection so don't increment the Index.
.ConvertToInlineShape
Else
' The shape is not a picture so increment the index (move to next shape).
Index = Index + 1
End If
End With
Loop
End Sub
试一试
For Each oShp In ActiveDocument.Shapes
oShp.Select
Selection.ShapeRange.ConvertToInlineShape
Next oShp
For i = 1 To ActiveDocument.Shapes.Count ActiveDocument.Shapes(1).ConvertToInlineShape 'index 值为 1,因为每次转换时计数都会减少 Next
我认为选择每个迭代形状是不必要的,并且可能会妨碍。
任何仍然需要答案的人,这对我有用:
For Count = 1 To 2
For Each oShp In ActiveDocument.Shapes
oShp.ConvertToInlineShape
Next oShp
Next Count
外循环的第一次迭代处理图片,第二次迭代处理绘图对象。不要问为什么!