Results 1 to 3 of 3

Replacing an Image Filename with the Actual Image

Threaded View

  1. #1
    Registered User
    Join Date
    03-11-2021
    Location
    Colombia
    MS-Off Ver
    2019
    Posts
    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
    Last edited by daniel21805; 03-12-2021 at 06:28 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Convert Image URLs To Actual Images - specific PNGs wont import
    By a_gunslinger in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-28-2020, 05:56 PM
  2. Insert Image With Image Filename In Another Cell - Excel
    By Angelq117 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-17-2016, 09:38 AM
  3. Replies: 1
    Last Post: 04-10-2016, 11:45 AM
  4. [SOLVED] Export chart as image - Input filename
    By soco3594 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-10-2016, 07:57 PM
  5. Get inserted image filename / name in a new column
    By jackooze in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-07-2016, 02:37 AM
  6. Automatic insertion of an image from its filename
    By Renaud22 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-22-2015, 11:00 AM
  7. display image file from concatenated filename
    By lydirot in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-14-2007, 05:08 AM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1