Convert all shape to image in Ms word with macro

孤街醉人 提交于 2019-12-03 22:34:29

问题


I write this macro to convert all shapes in document to image :

Sub AllShapeToPic()    
   For Each oShp In ActiveDocument.Shapes
    oShp.Select
    Selection.Cut
    Selection.PasteSpecial Link:=False, DataType:=wdPasteEnhancedMetafile, _
        Placement:=wdInLine, DisplayAsIcon:=False
   Next oShp

End Sub

But when i run it , none of shapes converted to image.
Whats wrong in my macro code ?


回答1:


Welcome to the wonderful world of manipulating the very collection you are looping through. The moment you cut, you are effectively removing the shape from the collection, altering your loop.

If you want to loop through shapes (or table rows or whatever) and delete something from that collection, simply go backwards:

Dim i As Integer, oShp As Shape

For i = ActiveDocument.Shapes.Count To 1 Step -1
    Set oShp = ActiveDocument.Shapes(i)
    oShp.Select
    Selection.Cut
    Selection.PasteSpecial Link:=False, dataType:=wdPasteEnhancedMetafile, _
        Placement:=wdInLine, DisplayAsIcon:=False
Next i

Alternatively for Tables (warning: untested!)

Dim tbl As Table

For i = ActiveDocument.Tables.Count To 1 Step -1
    Set tbl = ActiveDocument.Tables(i)
    tbl.Select
    Selection.Cut
    Selection.PasteSpecial Link:=False, dataType:=wdPasteEnhancedMetafile, _
        Placement:=wdInLine, DisplayAsIcon:=False
Next i

For equations: Equations are InlineShapes and have a "OMath" property. Use it to identify an equation object. Warning: untested

Dim equation As InlineShape

For i = ActiveDocument.InlineShapes.Count To 1 Step -1
    Set equation = ActiveDocument.InlineShapes(i)
    If equation.OMath > 0 Then
        equation.Select
        Selection.Cut
        Selection.PasteSpecial Link:=False, dataType:=wdPasteEnhancedMetafile, _
            Placement:=wdInLine, DisplayAsIcon:=False
    End If
Next i


来源:https://stackoverflow.com/questions/31537325/convert-all-shape-to-image-in-ms-word-with-macro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!