使用宏将 Ms word 中的所有形状转换为图像 [英] Convert all shape to image in Ms word with macro

查看:21
本文介绍了使用宏将 Ms word 中的所有形状转换为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 ?

推荐答案

欢迎来到操作您正在循环浏览的集合的奇妙世界.切割的那一刻,您实际上是从集合中删除了形状,从而改变了循环.

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

替代表格(警告:未经测试!)

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"属性.用它来识别方程对象.警告:未经测试

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

这篇关于使用宏将 Ms word 中的所有形状转换为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆