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

Selecting Sentences

Need to select an entire sentence at once? You can do so by creating a short macro that does the task for you, or you can ...

Discover More

Pulling AutoShape Text from a Worksheet Cell

AutoShapes can easily contain text—just click on the shape and start typing away. You may want the text in the ...

Discover More

ExcelTips Menu 2017 Archive (Table of Contents)

ExcelTips is a weekly newsletter that provides tips on how to best use Microsoft's word processing software. At ...

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)

Moving Objects without Snapping to the Grid

When you use the mouse to move objects around, they normally "snap" to the invisible grid that overlays your document. If ...

Discover More

Setting the Wrapping Default for Objects

Want to have objects such as text boxes and shapes always appear using some formatting you like? Here are some ideas on ...

Discover More

Scaling Graphics in a Macro

If you need to make sure that the graphics in a document are all scaled similarly, you'll love the macros presented in ...

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 six minus 0?

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.