Convert all shape to image in Ms word with macro

前端 未结 1 1569
庸人自扰
庸人自扰 2021-01-07 01:23

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

Sub AllShapeToPic()    
   For Each oShp In ActiveDocument.Shapes
    oShp.Select
    Selecti         


        
1条回答
  •  悲&欢浪女
    2021-01-07 01:31

    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
    

    0 讨论(0)
提交回复
热议问题