Inserting an Image On a Specific Page

by Allen Wyatt
(last updated June 21, 2014)

3

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

Controlling Sorting Order

When you sort information either in a table or the body of you document, Word follows a very specific set of rules to do the ...

Discover More

Using the ABS Function

Need to find the absolute value of a number? That's where the ABS function comes into play.

Discover More

Determining the Number of Fonts Available

When creating a macro, you may need to figure out how many fonts are available to Word. You can do this using the FontNames ...

Discover More

Do More in Less Time! Are you ready to harness the full power of Word 2013 to create professional documents? In this comprehensive guide you'll learn the skills and techniques for efficiently building the documents you need for your professional and your personal life. Check out Word 2013 In Depth today!

More WordTips (ribbon)

Converting Tables to Charts

Put numeric information in a table and you can then convert that information to a graphical chart using Microsoft Graph ...

Discover More

Nudging a Graphic

If you need to move a graphic just a little bit in one direction or another, you can do so by using the techniques in this ...

Discover More

Determining Picture Size in a Macro

When processing a document using a macro, you may need for your macro to figure out the sizes of the images in your document. ...

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 8 - 2?

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.