【问题标题】:Convert all shape to image in Ms word with macro使用宏将所有形状转换为 Ms word 中的图像
【发布时间】:2015-07-22 12:38:09
【问题描述】:

我编写了这个宏来将文档中的所有形状转换为图像:

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

但是当我运行它时,没有任何形状转换为图像。
我的宏代码有什么问题?

【问题讨论】:

    标签: vba ms-word ms-office


    【解决方案1】:

    欢迎来到操纵您正在循环的收藏的美妙世界。 在你切割的那一刻,你实际上是从集合中移除了形状,改变了你的循环。

    如果您想遍历形状(或表格行或其他)并从该集合中删除某些内容,只需向后退:

    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
    

    表的替代方案(警告:未经测试!)

    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
    

    对于方程式:方程式是 InlineShapes 并且具有“OMath”属性。用它来识别方程对象。 警告:未经测试

    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
    

    【讨论】:

    • 感谢您的回答,让我试试看:)
    • 抱歉,您能帮我看看如何处理表格和公式吗?
    • 注意:图像可以是“浮动”(=shape)或“随字符移动”(内联)。在第二种情况下,将“ActiveDocument.Shapes”替换为“ActiveDocument.InlineShapes”,否则宏将无法正常工作。还将“oShp As Shape”更改为“oShp As InlineShape”。或者最好为这两种类型设置两个单独的周期。
    猜你喜欢
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-07
    相关资源
    最近更新 更多