vba vba宏使所有形状和图片与文本内联
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15919815/
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
vba macro make all shapes and pictures to inline with text
提问by Sanghita
I want to write a macro which converts all the shapes and pictures in my word document to inline with text. The code that I am using converts all shapes(which are edited by drawing tools) to inline with text, but the table which are previously converted to pictures(edited by picture tools) or any other pictures are not text wrapping it to inline with text. Pasting the code I am using
我想编写一个宏,将 Word 文档中的所有形状和图片转换为内嵌文本。我正在使用的代码将所有形状(由绘图工具编辑)转换为内嵌文本,但先前转换为图片(由图片工具编辑)或任何其他图片的表格不是将其包装为与文本内嵌的文本. 粘贴我正在使用的代码
For Each oShp In ActiveDocument.Shapes
oShp.Select
Selection.ShapeRange.WrapFormat.Type = wdWrapInline
Next oShp
回答by Dave Williams
The first answer was a good start but there is a problem. For Each iterates over the collection but the collection is changed by ConvertToInlineShape. In other languages this throws an exception that the collection has been modified, here it just stops silently.
第一个答案是一个好的开始,但有一个问题。For Each 遍历集合,但集合已被 ConvertToInlineShape 更改。在其他语言中,这会引发集合已被修改的异常,在这里它只是静默停止。
To avoid this you need to either add each shape to another collection and iterate over them there. Or in the case of the below sample just keep track of the index manually.
为了避免这种情况,您需要将每个形状添加到另一个集合并在那里迭代它们。或者在以下示例的情况下,只需手动跟踪索引。
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
回答by Alan Stinchcombe
I think selecting each iterated shape is unnecessary and may be getting in the way.
我认为选择每个迭代形状是不必要的,并且可能会妨碍。
Anyone who still needs an answer, this works for me:
任何仍然需要答案的人,这对我有用:
For Count = 1 To 2
For Each oShp In ActiveDocument.Shapes
oShp.ConvertToInlineShape
Next oShp
Next Count
The first iteration of the outer loop tackles the pictures and the second iteration tackles the drawing objects. Don't ask why!
外循环的第一次迭代处理图片,第二次迭代处理绘图对象。不要问为什么!
回答by andrew
give this a go
试一试
For Each oShp In ActiveDocument.Shapes
oShp.Select
Selection.ShapeRange.ConvertToInlineShape
Next oShp