#AutoIt3Wrapper_run_debug_mode=Y ;Y/N use this to debug in console window <--- LOOK #include #include #include #include #include #include $version = "v1c" ;v1c - show more info ;v1b - cut down to showcase question ;v1a - original $oWord = _Word_Create() ;Create application object $testfile = "\Test for WordRangeTesting v1c.docx" $oDoc = _Word_DocOpen($oWord, @ScriptDir & $testfile, Default, Default, True) ;put test file in same directory as script If @error Then Exit MsgBox($MB_SYSTEMMODAL, "ERROR", "Error opening file = '" & @ScriptDir & $testfile & "'" & @CRLF & "@error = " & @error & ", @extended = " & @extended) ;--- TEST 1 - show that $vRange in _Word_DocRangeSet DOES set/extend range as noted in the documentation. Here we are assigning it to a return value. $oRange1 = _Word_DocFind($oDoc, "had", 0, Default, True) ;search entire doc from start and move toward end of document If $oRange1 = 0 Then MsgBox($MB_SYSTEMMODAL, "Information", "'had' not found.") $oRange1.Bold = False ;debug MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 1", "$oRange1='had' not bold") $oRange1.Bold = True ;so we can see it MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 1", "$oRange1='had' BOLD") $oRange1.Bold = False ;debug MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 1", "$oRange1='had' not bold") ;****IMPORTANT NOTE - _Word_DocRangeSet EXTENDS PASSED PARAMETER $oRange1 as documented in routine !!!!!!!!!!! ;as proof you can run this line below and $oRange1 will be extended with NO assignment ;_Word_DocRangeSet($oDoc, $oRange1, $wdCharacter, 0, $wdParagraph, 1) ;extend range to and including paragraph character $oRange1Extended = _Word_DocRangeSet($oDoc, $oRange1, $wdCharacter, 0, $wdParagraph, 1) ;extend range to end and including paragraph character ;so if my understanding is correct, $oRange1 is extended AND $oRange1Extended also represents the extended range. I'm assuming they are independent. Perhaps no so based on Test 3 below. $oRange1.Bold = False ;debug MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 1", "extended $oRange1='had' not bold") $oRange1.Bold = True ;so we can see it MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 1", "extended $oRange1='had' BOLD (extended to end)") $oRange1.Bold = False ;debug MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 1", "extended $oRange1='had' not bold") ;and show the assigned return object $oRange1Extended.Bold = False ;debug MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 1", "$oRange1Extended 'had' to end not bold") $oRange1Extended.Bold = True ;so we can see it MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 1", "$oRange1Extended 'had' to end BOLD") $oRange1Extended.Bold = False ;debug MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 1", "$oRange1Extended 'had' to end not bold") ;--- TEST 2 - show that $vRange in _Word_DocRangeSet DOES set/extend range as noted in the documentation - here WITHOUT assigning it to a return value ;okay need to start another test so reset $oRange1 to another range $oRange1 = _Word_DocFind($oDoc, "fleece", 0, Default, True) ;search entire doc from start and move toward end of document If $oRange1 = 0 Then MsgBox($MB_SYSTEMMODAL, "Information", "'fleece' not found.") $oRange1.Bold = False ;debug MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 2", "$oRange1='fleece' not bold") $oRange1.Bold = True ;so we can see it MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 2", "$oRange1='fleece' BOLD") $oRange1.Bold = False ;debug MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 2", "$oRange1='fleece' not bold") ;****IMPORTANT NOTE - _Word_DocRangeSet EXTENDS PASSED PARAMETER $oRange1 as noted in documentation. !!!!!!!!!!! ;as proof you can run this line below and $oRange001 will be extended with NO assignment _Word_DocRangeSet($oDoc, $oRange1, $wdCharacter, 0, $wdParagraph, 1) ;extend range to and including paragraph character :<<< WITHOUT assign returned value $oRange1.Bold = False ;debug MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 2", "extended $oRange1='fleece' not bold") $oRange1.Bold = True ;so we can see it MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 2", "extended $oRange1='fleece' BOLD (extended to end)") $oRange1.Bold = False ;debug MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 2", "extended $oRange1='fleece' not bold") ;should fail as $oRange001Extended not defined ;~ $oRange001Extended.Bold = False ;debug ;~ MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 2", "$oRange001Extended.Bold = False") ;~ $oRange001Extended.Bold = True ;so we can see it ;~ MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 2", "$oRange001Extended.Bold = True") ;~ $oRange001Extended.Bold = False ;debug ;~ MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 2", "$oRange001Extended.Bold = False") ;--- TEST 3 - show where I don't understand what's going on ;okay need to start another test so reset $oRange1 to 003 $oRange1 = _Word_DocFind($oDoc, "003", 0, Default, True) ;search entire doc from start and move toward end of document ;show it $oRange1.Bold = False ;debug MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 3", "should be 003 $oRange1.Bold = False") $oRange1.Bold = True ;so we can see it MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 3", "should be 003 $oRange1.Bold = True") $oRange1.Bold = False ;debug MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 3", "should be 003 $oRange1.Bold = False") ;$oRange1Extended not affected $oRange1Extended.Bold = False ;debug MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 3", "$oRange1Extended 'had' to end not bold") $oRange1Extended.Bold = True ;so we can see it MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 3", "$oRange1Extended 'had' to end BOLD") $oRange1Extended.Bold = False ;debug MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 3", "$oRange1Extended 'had' to end not bold") ;now from above we see that assigned a return value or not $vRange gets affected. ;now I want to keep a copy of the original range and assign the extended to another range variable name ;so try using a temp variable assigned to a range and see if it's extended but leaves $oRange1 unaltered $oTemp = $oRange1 ;check to see if object assignment works $oTemp.Bold = False ;debug MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 3", "should be 003 $oTemp.Bold = False") $oTemp.Bold = True ;so we can see it MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 3", "should be 003 $oTemp.Bold = True") $oTemp.Bold = False ;debug MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 3", "should be 003 $oTemp.Bold = False") ;and let's make sure $oRange1 still working $oRange1.Bold = False ;debug MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 3", "should be 003 $oRange1.Bold = False") $oRange1.Bold = True ;so we can see it MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 3", "should be 003 $oRange1.Bold = True") $oRange1.Bold = False ;debug MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 3", "should be 003 $oRange1.Bold = False") ;okay so here's the problem ************ ;let's extend $oTemp as we want to leave $oRange1 as it was. We don't need to assign _Word_DocRangeSet result. _Word_DocRangeSet($oDoc, $oTemp, $wdCharacter, 0, $wdParagraph, 1) ;extend range to and including paragraph character - TRYING to leave $$oRange1 alone <<< TRUE ?? NO NO NO see below $oTemp.Bold = False ;debug MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 3", "should be 003 extended to end of line $oTemp.Bold = False") $oTemp.Bold = True ;so we can see it MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 3", "should be 003 extended to end of line $oTemp.Bold = True") $oTemp.Bold = False ;debug MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 3", "should be 003 extended to end of line $oTemp.Bold = False") ;checking $oRange1 which should NOT be extended $oRange1.Bold = False ;debug MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 3", "should be 003 only $oRange1.Bold = False") $oRange1.Bold = True ;so we can see it MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 3", "should be 003 only $oRange1.Bold = True" & @CRLF& "Why is it extended? $oRange1 was defined as 003 and only $oTemp was extended.") $oRange1.Bold = False ;debug MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 3", "should be 003 only $oRange1.Bold = False") ;*********** DANG IT - THE ORIGINAL RANGE ***HAS*** BEEN EXTENDED - *** clearly I don't understand what's going on. *** ;perhaps the assignment is really a namespace pool and they all point to the same structure like aliases ;--- TEST 4 - from documentation we can extend a selection ;okay need to start another test so reset $oRange001 to 003 $oRange1 = _Word_DocFind($oDoc, "005", 0, Default, True) ;search entire doc from start and move toward end of document $oRange1.Bold = False ;debug MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 4", "should be 005 $oRange1.Bold = False") $oRange1.Bold = True ;so we can see it MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 4", "should be 005 $oRange1.Bold = True") $oRange1.Bold = False ;debug MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 4", "should be 005 $oRange1.Bold = False") ;try using select to extend i.e. current selection is the range $oRange1.Select MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 4", "Range1 '005' should be selected.") $oX = _Word_DocRangeSet($oDoc, 0, $wdCharacter, 0, $wdParagraph, 1) ;extend range to and including paragraph character - TRYING to leave $$oRangeFound alone <<< TRUE ?? YES YES YES MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 4", "Range1 selection extended. Assign it to $oX") ;this should show the extended $oX $oX.Bold = False ;debug MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 4", "(extended) $$oX.Bold = False") $oX.Bold = True ;so we can see it MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 4", "(extended) $$oX.Bold = True") $oX.Bold = False ;debug MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 4", "(extended) $$oX.Bold = False") ;now check that original $oRange1 HAS NOT BEEN EXTENDED $oRange1.Bold = False ;debug MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 4", "$oRange1.Bold = False") $oRange1.Bold = True ;so we can see it MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 4", "$oRange1.Bold = True" &@CRLF& "OKAY - using .Select to extend does NOT affect original Range") $oRange1.Bold = False ;debug MsgBox($MB_SYSTEMMODAL, "DEBUG TEST 4", "$oRange1.Bold = False") ;OKAY - using .Select to extend does NOT affect original Range Exit