3
votes

Right now I have a Word macro that moves an image in front of specific text by copying and pasting it to that location. This works pretty well, but it is costly. If I have 1,000s of images in my word doc it could take 30 minutes to run the macro.

There has to be a better way right?

Can I move the image anchor without copy/pasting the entire image?

My end goal is to take text + an image that is aligned in a table (text left, image right), break it out of the table, but maintain that left/right nature.

Specifically, I am looking for images in tables (row 1 column 2) and want to move them to the beginning of the text in that same table (1st column, 1st row). Here is a snippet:

For Each shape In innerTable.Cell(1, 2).Range.InlineShapes
    If shape.Type = wdInlineShapePicture Then
        shape.Select
        Selection.Cut
        innerTable.Range.Paragraphs(1).Range.Characters(1).Paste
        'Do it only for the 1st image found:
        Exit For
    End If

Next

Note, I am leaving out some safety checks for simplicity sake (this assumes I already have found a table of valid size, etc.

1
This is only possible if the target is on the same page. In that case, a Shape can be moved by changing its Top and Left properties. Unfortunately (extremely), there is no way to move an image by changing the anchor point. So the only way to move an image to another page (or story) is using copy/paste. - Cindy Meister
That is unfortunate. My end goal is to take text + an image that is aligned in a table (text left, image right), break it out of the table, but maintain that left/right nature. Any way to do that without the copy/paste? I was moving it to the beginning of the text, applying a specific alignment with "Square" wrap and then breaking it from the table - jpsnow72

1 Answers

1
votes

It is only possible to move a Shape if the target is on the same page. In that case, a Shape can be moved by changing its Top and Left properties. Unfortunately (extremely), there is no way to move an image by changing the anchor point. So the only way to move an image to another page (or story) is using copy/paste.

If an InlineShape is to be moved, just assign InlineShape.Range.FormattedText to the target Range. Or, extend a range with text to also include the InlineShape. As far as Word is concerned, an InlineShape is a character.

To achieve the stated end goal

take text + an image that is aligned in a table (text left, image right), break it out of the table, but maintain that left/right nature

use a table with an additional column, on the right. Put the image in it as an InlineShape. Then the entire row can be handled, for example as follows.

This creates a new row in the desired position, copies the Range.FormattedText of the row to be moved to the new row. Removes the additional row this creates and also deletes the original row.

Sub MoveRow()
    Dim tbl As Word.Table
    Dim rwToMove As Word.Row
    Dim rwTarget As Word.Row
    Dim rwBeforeTarget As Word.Row
    Dim posNewRow As Long

    posNewRow = 1
    Set rwToMove = Selection.Rows(1)
    Set tbl = rwToMove.Range.Tables(1)
    Set rwBeforeTarget = tbl.Rows(posNewRow)
    Set rwTarget = tbl.Range.Rows.Add(rwBeforeTarget) '(posNewRow)
    rwTarget.Range.FormattedText = rwToMove.Range.FormattedText
    tbl.Rows(posNewRow + 1).Delete
    rwToMove.Delete
End Sub