• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 62
  • Last Modified:

Replace text for image

   Could anyone please how i can replace à text string for an image in word?
So far i have tried this which provides The image every time regardless of The string.
Sub brief()
   Dim obj As LineShape
   Set obj = ActiveDocument.LineShapes.AddPicture(FileName:= _
    "C:\Users\Peter Hintzen\Documents\registratie documenten\handtekening.jpg", LinkToFile:=False, _
    SaveWithDocument:=True, Left:=-35, Top:=560)
       With obj
        .WrapFormat.Type = 3
        .ZOrder 5
       End With
   End Sub
1 Solution
This code will open an input box for you to enter your replacement string. It will then replace that string with the image defined by iPath.

  Sub TextToImage()
Dim sReplace As String, iPath As String

    iPath = "C:\Users\Peter Hintzen\Documents\registratie documenten\handtekening.jpg"
    sReplace = InputBox("Enter string to be replaced with image.", "String to Image Replacement", , vbOK)
    With Selection
        .HomeKey Unit:=wdStory

        With .Find
             .Text = sReplace

             Do While .Execute
                 Selection.InlineShapes.AddPicture FileName:=iPath, LinkToFile:=False, SaveWithDocument:=True

         End With
     End With

     With Selection.Find
         .Text = sReplace
         .Replacement.Text = ""
         .Forward = True
         .Wrap = wdFindContinue
         .Format = False
         .MatchCase = False
         .MatchWholeWord = True

     End With
     Selection.Find.Execute Replace:=wdReplaceAll

End Sub 

Open in new window

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now