# Function Reference

# _Word_DocRangeSet

Sets or modifies a range in a document

#include <Word.au3>
_Word_DocRangeSet ( $oDoc, $vRange [, $iStartUnit = Default [, $iStartCount = Default [, $iEndUnit = Default [, $iEndCount = Default]]]] )

## Parameters

$oDoc | Word document object
$vRange | Range to set or extend. You can pass an existing range which then is extended. Also valid is: 0 - uses the current selection as range -1 - sets the range start/end at the start of the document -2 - sets the range start/end at the end of the document
$iStartUnit | [optional] Unit by/to which the start position of the range is to be moved/set. Can be any of the WdUnit enumeration (default = $WdWord). When set to -1 the range is collapsed to the start of the range
$iStartCount | [optional] Number of units by/to which the start of range is moved/set. Positive numbers move the range forward in the document. If the position is moved forward to a position beyond the end position of the range, the range is collapsed and both the start and end positions are moved together(default = don't move the start position)
$iEndUnit | [optional] Unit by/to which the end position of the range is to be moved/set. Can be any of the WdUnit enumeration (default = $WdWord). When set to -1 the range is collapsed to the end of the range
$iEndCount | [optional] Number of units by/to which the end of the range is moved/set. If this number is negative, the end is moved backward. If the ending position overtakes the starting position, the range collapses and both positions move together (default = don't move the end position)

## Return Value

Success: | the modified range object.
Failure: | 0 and sets the @error flag to non-zero.
@error: | 1 - $oDoc is not an object 2 - $vRange has to be a range object, 0 or -1 3 - Error occurred when setting the start of the range. @extended is set to the COM error code 4 - Error occurred when setting the end of the range. @extended is set to the COM error code

## Example

    #include "Extras\HelpFileInternals.au3"

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

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

    ; Scene 1
    ; Move the start of the range to the next paragraph and extend the end by 2 words.
    ; Insert text before and after the range and display some statistics
    $oRange = _Word_DocRangeSet($oDoc, -1, $wdParagraph, 1, Default, 2)
    If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocRangeSet Example", _
            "Error setting/expanding range." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
    $oRange.Select
    MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocRangeSet Example", _
            "Range has been successfully set to the first two words in the second paragraph.")

    ; Insert some text before the range
    $oRange.InsertBefore("Inserted text at the start! ")
    $oRange.Select
    MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocRangeSet Example", "Inserted text at the start of the range.")

    ; Insert some text after the range
    $oRange.InsertAfter("inserted text at the end! ")
    $oRange.Select
    MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocRangeSet Example", "Inserted text at the end of the range.")

    ; Display some statistics
    Local $iWords = $oRange.ComputeStatistics(0)
    Local $iCharacters = $oRange.ComputeStatistics(3)
    MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocRangeSet Example", "The selected range has:" & @CRLF & _
            $iWords & " words " & @CRLF & $iCharacters & " characters.")

    ; Scene 2
    ; Move the end of the range one character to the left so the space to the next
    ; word isn't formatted and format the text bold, italic and underlined
    $oRange = _Word_DocRangeSet($oDoc, $oRange, Default, Default, $wdCharacter, -1)
    If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocRangeSet Example", _
            "Error setting/expanding range." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
    ; Format the range bold, italic und underline
    $oRange.Bold = True
    $oRange.Italic = True
    $oRange.Underline = True
    MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocRangeSet Example", "Added some formatting.")

    ; Scene 3
    ; Collapse the range to 0 length (insert mark) and insert a line break
    $oRange = _Word_DocRangeSet($oDoc, $oRange, Default, Default, -1, Default)
    If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocRangeSet Example", _
            "Error setting/expanding range." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
    $oRange.InsertBreak($wdLineBreak)
    MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocRangeSet Example", "Inserted a break.")

    ; Scene 4
    ; Move the range to the first character in the next line (space) and remove it
    $oRange = _Word_DocRangeSet($oDoc, $oRange, Default, Default, $wdCharacter, 1)
    If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocRangeSet Example", _
            "Error setting/expanding range." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
    $oRange.Delete
    MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocRangeSet Example", "Deleted a character.")