Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

Lorem ipsum dolor sit amet, consectetur adipiscing elit. 

c:\Users\userx\Documents\img_000180.jpg

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?

share|improve this question

1 Answer 1

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
'
'
    Selection.Find.ClearFormatting
    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.Find.Execute
    Selection.EscapeKey
    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.Cut
    Selection.InlineShapes.AddPicture FileName:= _
        FilePath _
        , LinkToFile:=False, SaveWithDocument:=True
    Selection.MoveRight Unit:=wdCharacter, Count:=1
End Sub
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.