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

Unwanted Lines on Printout

Do you have strange lines appearing at the corners of your printout? There are a number of reasons this could be happening, ...

Discover More

Unwanted Cover Pages with Print Jobs

When you print a document, do you get more than you bargained for? If you get extra pages printed either before or within ...

Discover More

Merge and Center Not Available

What are you to do if you are trying to format a worksheet, only to find out that one of the tools you need is not available? ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More WordTips (ribbon)

Best Quality for High Resolution Graphics

You want your documents to look as good as they can. If those documents include graphics, then you also need to make sure ...

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

A Picture Is Worth a Thousand Words

Nothing beats a screen shot when you are trying to convey information about using the computer. With just a couple of easy ...

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 8Mpixels. 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 7 - 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.