Mac – Inserting pictures automatically using their file paths in the text

macrosmicrosoft word

I have a text document where image paths are written explicitly such as:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. 


I am looking for some macro script that will look for these file paths in the document and insert these images into the Word document.

Do you know such a script?

Best Answer

I recorded a macro and changed it a bit in order to make it generic. It does what I need. Here is the script:

Sub replace_path_with_image()
' replace_path_with_image Macro
    With Selection.Find
        .Text = "c:\users"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    Dim Sel As Selection
    Set Sel = Application.Selection
    Dim FilePath As String
    If Sel.Type <> wdSelectionIP Then
        FilePath = Sel.Text
    End If
    Selection.InlineShapes.AddPicture FileName:= _
        FilePath _
        , LinkToFile:=False, SaveWithDocument:=True
    Selection.MoveRight Unit:=wdCharacter, Count:=1
End Sub
Related Question