Written by Allen Wyatt (last updated May 9, 2020)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365
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:
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 Word in Microsoft 365.
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!
Using graphics to add callouts to your graphics is a common occurrence in Word. Here's how to stop all those graphics ...
Discover MoreNothing beats a screen shot when you are trying to convey information about using the computer. With just a couple of ...
Discover MoreWhen you convert a document from a PDF to a Word file, you may end up with more than you bargain for. This tip discusses ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.
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.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments