Sign in to follow this  
Followers 0
Dieuz

Word Document, Text replacement

3 posts in this topic

Hi,

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?

Thanks,

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 = ""
        EndWith
        ; 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)
        EndIf
        $iFound += 1
    EndIf
WEnd

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

All files needed for a working example

_WordDocReplaceWithImage.zip

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  
Followers 0