Share via

VBA Code- Insert and resize image with square text wrap

Anonymous
2018-11-26T12:34:44+00:00

i am new to VBA and don't really understand too much as i have only learnt to record macros. I am looking to insert an image into a template and have it 4cms tall with the width relative to the original sizing. I also need to have the text wrap as square wrap. Can anyone help at all please.

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Jay Freedman 207.7K Reputation points Volunteer Moderator
2018-11-26T17:17:39+00:00

Here's a rather minimal macro to do what you asked for:

Sub InsertPictureSpecial()

    Dim strFilename As String

    Dim shpShape As Shape

    With Dialogs(wdDialogInsertPicture)

        .Display

        strFilename = .Name  ' full path of selected file

    End With

    If Len(strFilename) > 0 Then

        Set shpShape = ActiveDocument.Shapes.AddPicture( _

            FileName:=strFilename, _

            Anchor:=Selection.Range)

        With shpShape

            .LockAspectRatio = msoTrue

            .Height = CentimetersToPoints(4#)

            .WrapFormat.Type = wdWrapSquare

        End With

    End If

End Sub

The first part uses the built-in Insert > Picture dialog to let the user select a file. The .Display command shows the dialog, but when the user clicks the Insert button the dialog just closes without inserting the picture in the document. Instead, the next line captures the filename (including the full path) in a String variable.

The next part uses the filename to insert the picture as a floating Shape object (even if the user's Options setting says to insert pictures as In Line with Text). The object is "anchored" to the current location of the cursor (the Selection in VBA-speak). Then the macro locks the aspect ratio (the fraction height/width), sets the height to 4 cm, and sets the wrapping.

This macro could be expanded in several ways, such as setting the picture's position (its .Left and .Top values) relative to the anchor or to the page margins; using a different method of choosing the file to insert; and adding some error-trapping in case something goes wrong.

Was this answer helpful?

5 people found this answer helpful.
0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2018-11-27T00:56:49+00:00

    This worked a treat, thank you so much. i also appreciate you taking the time to talk through the steps as well.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments