# Function Reference

# _Word_DocAttach

Attaches to the first instance of a Word document where the search string matches based on the selected mode

#include <Word.au3>
_Word_DocAttach ( $oAppl, $sString [, $sMode = "FilePath" [, $iCase = 0]] )

## Parameters

$oAppl | Word object returned by a preceding call to _Word_Create()
$sString | String to search for
$sMode | [optional] Search mode to use. Valid modes are: "FileName" - Name of the open document "FilePath" - Full path to the open document (default) "Text" - Text in the body of the document
$iCase | [optional] Specifies case-sensitivity of function StringInStr() used for search mode "Text": 0 - not case-sensitive, using the user's locale (default) 1 - case sensitive 2 - not case sensitive, using a basic/faster comparison

## Return Value

Success: | a variable pointing to the document object.
Failure: | 0 and sets the @error flag to non-zero.
@error: | 1 - $oAppl is not an object 2 - $sString is empty 3 - $sMode is invalid. Must be "FilePath", "FileName" or "Text" 4 - No match could be found

## Remarks

This function only returns the object of the found document.
To access the corresponding Word application use: $oAppl = $oDoc.Application.

## Related

_Word_DocClose, _Word_DocGet

## Example

### Example 1

    #include "Extras\HelpFileInternals.au3"

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

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

    ; Attach to the test document by "FileName" and set focus to the window
    Local $oDoc = _Word_DocAttach($oWord, "Test.doc", "Filename")
    If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocAttach Example", _
            "Error attaching to '.\Extras\Test.doc' by 'FileName'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
    ; Set focus to the word document - $f_takeFocus parameter of the old _WordCreate function
    WinActivate($oWord.ActiveWindow.Caption & " - " & $oWord.Caption)
    MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocAttach Example", "Attach to document by 'FileName' successfull!" & @CRLF & _
            @CRLF & "Text of the attached document:" & @CRLF & $oDoc.Range().Text)

### Example 2

    #include "Extras\HelpFileInternals.au3"

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

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

    ; Attach to the test document by "Text"
    Local $oDoc = _Word_DocAttach($oWord, "Test", "Text")
    If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocAttach Example", _
            "Error attaching to '\Extras\Test.doc' by 'Text'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
    MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocAttach Example", "Attach to document by 'Text' successfull!" & _
            @CRLF & @CRLF & "Text of the attached document:" & @CRLF & $oDoc.Range().Text)