2

Word 文書内のすべての図形と画像をテキスト付きのインラインに変換するマクロを作成したいと考えています。私が使用しているコードは、すべての図形 (描画ツールで編集) をテキストでインラインに変換しますが、以前に画像 (画像ツールで編集) に変換されたテーブルまたはその他の画像は、テキストでインラインにラップするテキストではありません。 . 使用しているコードの貼り付け

For Each oShp In ActiveDocument.Shapes
   oShp.Select
   Selection.ShapeRange.WrapFormat.Type = wdWrapInline
Next oShp
4

4 に答える 4

2

最初の答えは良いスタートでしたが、問題があります。For Each はコレクションを反復処理しますが、コレクションは ConvertToInlineShape によって変更されます。他の言語では、コレクションが変更されたという例外がスローされますが、ここではサイレントに停止します。

これを回避するには、各シェイプを別のコレクションに追加して、そこで反復する必要があります。または、以下のサンプルの場合は、インデックスを手動で追跡します。

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
于 2014-08-04T15:29:44.007 に答える