Function Reference


_Word_DocFindReplace

Runs the specified find and replace operation

#include <Word.au3>
_Word_DocFindReplace ( $oDoc [, $sFindText = "" [, $sReplaceWith = "" [, $iReplace = $WdReplaceAll [, $vSearchRange = 0 [, $bMatchCase = False [, $bMatchWholeWord = False [, $bMatchWildcards = False [, $bMatchSoundsLike = False [, $bMatchAllWordForms = False [, $bForward = True [, $iWrap = $WdFindContinue [, $bFormat = False]]]]]]]]]]]] )

Parameters

$oDoc Word document object
$sFindText [optional] The text to be searched for. Use an empty string ("") to search for formatting only.
You can search for special characters by specifying appropriate character codes.
For example, "^p" corresponds to a paragraph mark and "^t" corresponds to a tab character (default = "")
$sReplaceWith [optional] The replacement text. To delete found text use an empty string ("").
You specify special characters and advanced search criteria just as you do for the Find argument (default = "")
$iReplace [optional] How many replacements are to be made: one, all, or none.
Can be anyWdReplace constant (default = $WdReplaceAll)
$vSearchRange [optional] Specifies the selection or range to search. Can be:
    -1 - Specifies the current selection
    0 - Specifies the entire document (default)
Any Word range object.
$bMatchCase [optional] If True the find is case sensitive (default = False)
$bMatchWholeWord [optional] If True only find entire words (default = False)
$bMatchWildcards [optional] If True the find text has special search operators (default = False)
$bMatchSoundsLike [optional] If True finds words that sound similar to the find text (default = False)
$bMatchAllWordForms [optional] If True finds all forms of the find text
(e.g. "sit" locates "sitting" and "sat") (default = False)
$bForward [optional] True to search forward (toward the end of the document) (default = True)
$iWrap [optional] True wraps when the bottom or top of the document, selection or range is reached.
Can be one of the WdFindWrap constants (default = $WdFindContinue)
$bFormat [optional] True to have the find operation locate formatting in addition to or instead of the find text (default = False)

Return Value

Success: 1.
Failure: 0 and sets the @error flag to non-zero.
@error: 1 - $oDoc is not an object
2 - $vSearchRange is not an object
3 - An error occurred when the replace operation was executed. @extended is set to the COM error code

Related

_Word_DocFind

Example

Example 1

#include <MsgBoxConstants.au3>
#include <Word.au3>

; Create application object
Local $oWord = _Word_Create()
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFindReplace Example", _
                "Error creating a new Word application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
; Open test document read-only
Local $oDoc = _Word_DocOpen($oWord, @ScriptDir & "\Extras\Test.doc", Default, Default, True)
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFindReplace Example", _
                "Error opening '.\Extras\Test.doc'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

; Change "test document" to "test document with replaced text".
_Word_DocFindReplace($oDoc, "test document", "test document with replaced text")
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFindReplace Example", _
                "Error replacing text in the document." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFindReplace Example", "Text successfully replaced.")

Example 2

#include <MsgBoxConstants.au3>
#include <Word.au3>

; Create application object
Local $oWord = _Word_Create()
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFindReplace Example", _
                "Error creating a new Word application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
; Open test document read-only
Local $oDoc = _Word_DocOpen($oWord, @ScriptDir & "\Extras\Test.doc", Default, Default, True)
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFindReplace Example", _
                "Error opening '.\Extras\Test.doc'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

; Replace paragraph control character with paragraph + text + paragraph.
; Only change first occurrence.
_Word_DocFindReplace($oDoc, "^p", "^pInserted Line^p", $wdReplaceOne)
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFindReplace Example", _
                "Error replacing text in the document." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFindReplace Example", "Paragraph control character successfully replaced.")