Inserting an Image On a Specific Page

Written by Allen Wyatt (last updated February 14, 2024)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365


1

Suman needs to use a macro to insert an image on page 10 of a particular document. He wonders about the best way, within the macro, to go about this.

If you want the image to be the only thing on page 10, then the task is rather easy: Use the macro to locate page 10, insert the image, and then insert a page break after the image. This then bumps everything after the image up by a page, such that the old page 10 becomes page 11, page 11 becomes page 12, and so on.

Sub InsertImage1()
    Dim PicPath As String

    PicPath = "C:\My Pictures\My Scans\scan0002.jpg"
    ActiveDocument.GoTo(What:=wdGoToPage, Count:=10).Select
    Selection.InlineShapes.AddPicture FileName:=PicPath, _
      LinkToFile:=False, SaveWithDocument:=True
    Selection.InsertBreak Type:=wdPageBreak
End Sub

The macro inserts the image as an inline shape. If you prefer, you could insert the image as a floating shape, which would allow text to wrap around the image. The following macro does that, as well as centering the image in the center of the page:

Sub InsertImage2()
    Dim PicPath As String
    Dim aShape As Shape

    PicPath = "C:\My Pictures\My Scans\scan0002.jpg"
    ActiveDocument.GoTo(What:=wdGoToPage, Count:=10).Select
    Set aShape = Selection.InlineShapes.AddPicture(FileName:=PicPath, _
      LinkToFile:=False, SaveWithDocument:=True).ConvertToShape
    With aShape
        .WrapFormat.Type = wdWrapTight
        .RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
        .RelativeVerticalPosition = wdRelativeVerticalPositionPage
        .Top = wdShapeCenter
        .Left = wdShapeCenter
        .Select
    End With
End Sub

If you wanted your image to replace an existing image (perhaps with an updated image), then you can do so by using a bit more complex macro. The following macro will locate the first image on the desired page and replace it with an inline shape.

Sub ReplaceImage()
    Dim r1 As Range
    Dim r2 As Range
    Dim nPages As Long
    Dim nP As Long
    Dim PicPath As String
    Dim sTemp As String

    PicPath = "C:\My Pictures\My Scans\scan0002.jpg"
    nP = 10    ' Page number on which to replace the image

    sTemp = ""
    nPages = ActiveDocument.ComputeStatistics(statistic:=wdStatisticPages)
    If nP > nPages Then
        sTemp = "You are trying to go to a non-existent page"
    Else
        Set r1 = ActiveDocument.GoTo(What:=wdGoToPage, Count:=nP)
        If nP = nPages Then
            Set r2 = ActiveDocument.Range
            r1.End = r2.End
        Else
            Set r2 = ActiveDocument.GoTo(What:=wdGoToPage, Count:=nP + 1)
            r1.End = r2.Start - 2
        End If
        If r1.InlineShapes.Count = 0 Then
            sTemp = "Page " & nP & " does not have an inline picture"
        Else
            r1.InlineShapes(1).Select
            Selection.InlineShapes.AddPicture FileName:=PicPath, _
              LinkToFile:=False, SaveWithDocument:=True
        End If
    End If
    If sTemp > "" Then MsgBox sTemp
End Sub

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (13139) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Finding Text Not Using a Particular Font

Word makes it easy to find text that uses a particular font or font characteristics. What it doesn't do is make it easy ...

Discover More

Setting a Default for the Object Browser

Does it bother you that when you press Ctrl+Page Up or Ctrl+Page Down you aren't always taken to the top of the previous ...

Discover More

Deriving a List of Albums by a Music Artist

You can use Excel to keep what is essentially a small, simple database of information. Getting information from the ...

Discover More

The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!

More WordTips (ribbon)

Graphics Marked as Changed when Comparing Documents

The tools on the Review tab of the ribbon allow you to do all sorts of markup and comparisons of documents. When ...

Discover More

Using the Drawing Grid

One of the lesser-known drawing tools provided in Word is the drawing grid. You can easily turn this feature on and use ...

Discover More

The Changing Relationship of WordArt and Text Boxes

Two of the long-time features in Word are text boxes and WordArt. You might not think these two are related, but they are ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 8 - 5?

2020-11-20 08:29:08

Shell

How would one centre a floating shape both vertically & horizontally please?

# Add A Floating Shape (image)
$objShape = $objDoc.Shapes.AddPicture("$imgFileName")

# centre horizontally
$wdShapeCenter = -999995
$msoAlignCenters = 1
$objShape.Left = $wdShapeCenter

# centre vertically - DOES NOT WORK, tried 1 to 5
$objShapeRange = $objDoc.Shapes.Range(1)
$objShapeRange.Align($msoAlignCenters, $true)


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.