PDA

View Full Version : [SOLVED:] Replacing an Image Filename with the Actual Image



daniel21805
03-12-2021, 10:05 AM
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

macropod
03-12-2021, 02:43 PM
Cross-posted at: https://www.excelforum.com/word-formatting-and-general/1343836-replacing-an-image-filename-with-the-actual-image.html
Please read VBA Express' policy on Cross-Posting in Rule 3: http://www.vbaexpress.com/forum/faq.php?faq=new_faq_item#faq_new_faq_item3

daniel21805
03-12-2021, 03:29 PM
Thank you very much, it was my fault i did not notice it was the same forum. I just marked the other as "SOLVED" (i did this because I could not delete it). Please let me know if it is ok.

Regards

macropod
03-12-2021, 09:15 PM
i did not notice it was the same forum.
It isn't the same forum - which is the whole point. Please read the Rule.

daniel21805
03-12-2021, 09:31 PM
Ok, I understand, but What can I do if I can not delete any of my posts? I am really sorry about it

I have marked this post in the other forum as "solved" to avoid anyone works on it: https://www.excelforum.com/word-formatting-and-general/1343836-replacing-an-image-filename-with-the-actual-image.html

macropod
03-12-2021, 09:53 PM
You clearly either haven't read or understood the rules on either forum, which in both cases say nothing about having to delete threads or otherwise restrict yourself to just one forum.