by Allen Wyatt
(last updated June 21, 2014)
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.
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!
Getting graphics to appear right where you want them in relation to the text in your document can be a challenge. One ...Discover More
Need your graphics to be larger or smaller than they first appear when you insert them in a document? Your best bet may ...Discover More
If you have a graphic that has text wrapping around it, you might want a way to modify the wrapping path used by Word. ...Discover More
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.