Jump to content
Sign in to follow this  

Word Document, Text replacement

Recommended Posts



What would be the best way to replace text in a word document with an image. In word there's already a function to replace text by text but I would like to replace texte by an image. Is there any autoit script related to that?


Share this post

Link to post
Share on other sites

I wanted to make somewhat of an advanced example, so sorry if it is confusing.

#include <Word.au3>

$iFound = 0
$oWordApp = _WordCreate(@ScriptDir & "\Test.doc")
$oDoc = _WordDocGetCollection($oWordApp, 0)
; Specifies a range object of the entire document
$oRng = $oDoc.Range

; Loop through found items
While Not @error
    ; Search for "<Picture?>" in the range specified above
    ; After searching the range is changed to the found text
    _WordDocFindReplace($oDoc, "\<Picture?\>", "", 0, $oRng, 0, 0, 1, 0, 0, 1, 0)
    If Not @error Then
        ; Duplicates range leaving the original unchanged
        $oTempRng = $oRng.Duplicate
        ; Save the text of the found range
        ; Set the text to null
        With $oTempRng
            $sText = .Text
            .Text = ""
        ; Find the text between < & >
        $aMatch = StringRegExp($sText, "\<(.*)\>", 1)
        If Not @error Then
            ; Replace the range with the correct image
            _WordDocAddPicture($oDoc, @ScriptDir & "\" & $aMatch[0] & ".jpg", 0, 1, $oTempRng)
        $iFound += 1

If $iFound > 0 Then
    MsgBox(0, "Success", "Found " & $iFound & " occurrences.")
    MsgBox(0, "Error", "Not Found")

All files needed for a working example


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.