Consulting

Results 1 to 6 of 6

Thread: Replacing an Image Filename with the Actual Image

  1. #1

    Replacing an Image Filename with the Actual Image

    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

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Cross-posted at: https://www.excelforum.com/word-form...ual-image.html
    Please read VBA Express' policy on Cross-Posting in Rule 3: http://www.vbaexpress.com/forum/faq...._new_faq_item3
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    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

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by daniel21805 View Post
    i did not notice it was the same forum.
    It isn't the same forum - which is the whole point. Please read the Rule.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    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-form...ual-image.html
    Last edited by daniel21805; 03-12-2021 at 09:42 PM.

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •