Replacing an Image Filename with the Actual Image

Written by Allen Wyatt (last updated May 1, 2024)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and 2021


4

Steve has a document with text that indicates where an image should be placed. This text consists of the image name (such as "image01.jpg") within parentheses. He is looking for a way to automatically replace each image name with the actual image.

This cannot be done with the regular Find and Replace capabilities of Word, but it can be done using a macro. The idea would be to search through the document for the marker text (the image names) and, if one is found, grab the image name and replace the marker text with the actual image. Here's a macro that implements these steps:

Sub ReplaceImages()
    Dim sMarkerText As String
    Dim sFigName As String
    Dim sFigPath As String

    ' Change to the path to the pictures, with a trailing slash.
    sFigPath = "C:\Users\Steve\Pictures\"

    ' Change to marker text. Can include wildcards.
    sMarkerText = "(image??.jpg)"

    ' Search through document for marker text
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = sMarkerText
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    While Selection.Find.Found
        ' Found a match, so grab name
        ' Need to adjust for parens in marker text
        sFigName = Mid(Selection, 2, Len(Selection) - 2)

        ' Delete the marker text
        Selection.Delete

        ' Insert the picture
        Selection.InlineShapes.AddPicture FileName:= _
          sFigPath & sFigName, LinkToFile:=False, _
          SaveWithDocument:=True
        Selection.Find.Execute
    Wend
End Sub

There are two things you need to modify in the macro code: the values stored in the sFigPath and sMarkerText variables. The sMarkerText contents, as shown, will match any parentheses in which the word "image" is followed by two characters (such as 01, 02, 97, or XY) and the ".jpg" extension.

The Find method of the Selection object implements an actual Find action for the very first occurrence of the marker text. If it is found, then the code in the While...Wend loop comes into play. This grabs the filename and assigns it to the sFigName variable. Then the found marker text is deleted and an inline image inserted in its place. Finally, the Selection.Find.Execute line finds the next occurrence of the marker text, if any.

The macro inserts images inline and does not do any additional processing on them.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (10135) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, and 2021.

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

Confirming File Conversions

Open a file that isn't a Word document and Word will still try to convert it to a Word document. If you want Word to let ...

Discover More

Finding and Replacing in Text Boxes

Finding and replacing information in a worksheet is easy. Finding and replacing in other objects (such as text boxes or ...

Discover More

Ignoring Selected Words when Sorting

If you use Excel to maintain a list of text strings (such as movie, book, or product titles), you may want the program to ...

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)

Changing the Size of a Graphic

Word allows you to add more than text to your documents; you can also add graphics. Once added, you can modify the size ...

Discover More

Graphics and Line Height

If the in-line graphics in your document appear "chopped off," it could be directly related to the formatting within the ...

Discover More

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
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}] (all 7 characters, in the sequence shown) 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 7 - 7?

2024-05-17 02:10:06

Alex May

I think there are a couple of errors in the above (which is otherwise brilliant and a much needed way to integrate WhatsApp photos back into a backup of messages), or at least I need to tweak a couple of things to make it work.
These were:
".MatchWildcards = False" should be ".MatchWildcards = True"
and
sMarkerText = "(image??.jpg)" should be sMarkerText = "\(image??.jpg\)"


2024-05-03 23:18:39

Tomek

@Robert Brown:
Did you mean comments by others or the tip itself?
In any case, instead of saving the comment/tip, just copy the relevant part and paste it into any text document (in Notepad, Word, or even directly into the VBA Editor), then save the file you pasted it in.
BTW, pdf is not the best format for this, but with a proper pdf editor it can be done too.


2024-05-01 21:19:13

Robert Brown

Suppose I want to save one of your comments that includes the text of a macro.
If I try to save it as a PDF, the macro text disappears.


2021-03-10 14:31:45

Daniel Escobar

URGENT: I have tried to apply this macro however the wildcards "(image??.jpg)" do not work, the macro only works when I write the name of the image, e.g. "(image01.jpg)" Could you please help me with this? Thank you very much.


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.

Videos
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.