AutoIT Counting Words In Text Selection : I have written a function in AutoIT to count the number of words, characters with and without spaces, lines, and estimated speaking time (assuming a speaking speed of two words per second) in a segment of text selected by the user.  This selected text is highlighted in blue.

It works just great 90% of the time. However, if the starting position for the selection is 0 in the input string (at the upper left-hand corner of the top of the file), the function returns 0 for the number of characters, 0 for the number of words, 0 for everything, even when the main function that calculates the number of words in the file from start to finish works perfectly.  I cannot figure out why.  Here is the code:

           $input_str = GUICtrlRead($Textbox)
    	   $selpos = _GUICtrlEdit_GetSel($Textbox)
    	   MsgBox($MB_OK, $selpos[0], $selpos[1])
    	   $selstring = StringMid($input_str, $selpos[0], ($selpos[1]-$selpos[0]))
    	   $WordArray = StringRegExp($selstring, "[\s\.:;,]*([a-zA-Z0-9-_]+)[\s\.:;,]*", 3)
    	   $SingleQuotes = StringRegExp($selstring, "'", 3)
    	   $Result = ""
    		 $Seconds = (UBound($WordArray) - UBound($SingleQuotes)) / 2
    
    		 If $Seconds >= 3600 Then
    			If $Seconds / 3600 >= 2 Then
    			   $Result = $Result & Int($Seconds/3600) & " hours "
    			Else
    			   $Result = $Result & Int($Seconds/3600) & " hour "
    			EndIf
    		 EndIf
    
    		 If Mod($Seconds, 3600) >= 60 Then
    			If $Seconds / 60 >= 2 Then
    			   $Result = $Result & Int(Mod($Seconds, 3600) / 60) & " minutes "
    			Else
    			   $Result = $Result & Int(Mod($Seconds, 3600) / 60) & " minute "
    			EndIf
    		 EndIf
    
    		 If Mod($Seconds, 60) > 0 Then
    			If Mod($Seconds, 60) >= 2 Then
    			   $Result = $Result & Int(Mod($Seconds, 60)) & " seconds "
    			Else
    			   $Result = $Result & Int(Mod($Seconds, 60)) & " second "
    			EndIf
    		 EndIf
    	   MsgBox($MB_OK, "Selection Properties", "Number of characters (with spaces): " & StringLen($selstring) & @CRLF  & "Number of Characters (without spaces): " & StringLen(StringStripWS($selstring, 8)) & @CRLF & "Number of words: " & (UBound($WordArray) - UBound($SingleQuotes)) & @CRLF  & "Number of lines: " & _GUICtrlEdit_GetLineCount($selstring) & @CRLF  & "Estimated speaking time: " & $Result)

Any assistance anyone can provide me would be greatly appreciated.