I have a document a with text that indicates where an image should be placed. This text consists of the image name (such as "image01.jpg") within parentheses. Looking for a solution I got a macro from a website and 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. The problem is that this macro does not work with wildcards it only works with the proper name of one image. Can you please tell me what is wrong with it?
Thank you very much in advance.
Sub ReplaceImages()
Dim sMarkerText As String
Dim sFigName As String
' Change to the path to the pictures, with a trailing slash.
sFigPath = "C:\Desktop\Images\"
' Change to marker text. Can include wildcards.
sMarkerText = "(image??.png)"
' 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
Bookmarks