Inserting an Image On a Specific Page

by Allen Wyatt
(last updated June 21, 2014)

4

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 the easiest way is to simply use a bookmark to "mark" the existing image. (You'll need, in other words, to bookmark the current image on page 10. It is possible, of course, that the bookmark could "float" to a different page as you add or remove information from the document.) The following macro will locate the bookmark (named Bk1) and replace it with an inline shape.

Sub InsertImage3()
    Dim PicPath As String
    Dim BmkName As String

    PicPath = "C:\My Pictures\My Scans\scan0002.jpg"
    BmkName = "Bk1"

    With ActiveDocument
        If .Bookmarks.Exists(BmkName) Then
            Dim WrdPic As Word.InlineShape
            Set WrdPic = .Bookmarks(BmkName).Range _
              .InlineShapes.AddPicture(PicPath, False, True)
        End If
    End With
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, and 2013.

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

Preparing Files for a Commercial Printer

Sometimes you may want to send Word documents to a commercial printer for professional mass production. Doing this ...

Discover More

Moving Drawing Objects

Add a drawing object to a worksheet and chances are good you'll need to move it in some way. Here's how to use the mouse ...

Discover More

Dates with Periods

You may want Excel to format your dates using a pattern it doesn't normally use—such as using periods instead of ...

Discover More

Learning Made Easy! Quickly teach yourself how to format, publish, and share your content using Word 2013. With Step by Step, you set the pace, building and practicing the skills you need, just when you need them! Check out Microsoft Word 2013 Step by Step today!

More WordTips (ribbon)

Understanding Grayscale Images

Word allows you to easily add images to your documents. For documents intended for monochrome printers, grayscale images ...

Discover More

Counting All Graphics

Need to know how many graphics a document contains? Getting at the true number may take a little more work than it first ...

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
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}] 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 five less than 6?

2018-03-02 16:23:12

roland

Nice!

I have a vertical line that I added to a Word document, but the line moves when I add a table next. How do I keep the line fixed to the page? There is an option to do it in the drop down for drawing tools/format/warp text/Fix Position on Page. How do I do it programmatically?


2014-08-21 01:45:44

K.Vee.Shanker.

Hi Ken Endacott,

It's very nice of you to have shared your tip for the benefit of others. Thanks. That you've also considered all possibilities during execution is praiseworthy indeed!

Sorry for the delayed appreciation! I happened to see your tip only today (21st Aug 2014).


2014-07-05 08:21:22

Ken Endacott

Rather than bookmark a picture so that it can be replaced, a better method would be to simply replace whatever picture is on the page. The following macro will select the contents of page 10 and if it has one or more inline pictures replace the first picture with the new picture. Additional statements could be added to set the new picture to the same size as the old picture.

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

PicPath = "C:My PicturesMy Scansscan0002.jpg"
nP = 10

nPages = ActiveDocument.ComputeStatistics(statistic:=wdStatisticPages)
If nP > nPages Then
MsgBox "You are trying to go to a non existent page"
Exit Sub
End If
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
MsgBox "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 Sub


2014-06-26 01:04:25

Suman

Hi Allen,

Thanks you very much for your code, it really helpful.


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.

Newest Tips
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.