+2  A: 

This doesn't help immediately, but addresses a bigger problem

This code needs to be refactored. It will cause problems for you down the line. You have copy-pasted code that will be a pain to take care of. And also, it makes it harder for others to help.

Here is a suggestion for a refactoring (Not Tested)

    private mshtml.IHTMLTxtRange SelectCSNumbers(mshtml.IHTMLTxtRange myRange)
    {
        mshtml.IHTMLTxtRange tmpRange = myRange.duplicate();
        string[] strInt = tmpRange.text.Split(',');
        bool result = false;
        result = CheckText(tmpRange, strInt, result);

        if (result && count==0)//
        {

            //Expand the Range with a single Character
            tmpRange.expand("character");

            if (tmpRange.text.Length > myRange.text.Length)
            {
                if (tmpRange.text.IndexOf(' ') == -1)  //if no space is found that means the selection is not proper 
                {

     SomeOtherFunction(tmpRange);
                }
                else if (tmpRange.text.IndexOf(' ') != -1)
                {
                    tmpRange = myRange.duplicate();
                    tmpRange.moveStart("character", -1);

                    SomeOtherFunction(tmpRange);

                }

            }
            else if (tmpRange.text.Length == myRange.text.Length)
            {
                tmpRange = myRange.duplicate();
                tmpRange.moveStart("character", -1);
                if (tmpRange.text.Length == myRange.text.Length)
                {
                    //tmpRange = null;
                    return tmpRange.duplicate();//here it should terminate
                    count++;
                }
                else if (tmpRange.text.IndexOf(' ') == -1)  //if no space is found that means the selection is not proper 
                {
                    SomeOtherFunction(tmpRange);
                }

            }
        }
        return tmpRange.duplicate();
    }


 private void SomeOtherFunction(mshtml.IHTMLTxtRange tmpRange)
 {
  if (tmpRange.text.IndexOf(',') == -1)//if NO Comma is found
  {
   if (tmpRange.text.IndexOf('.') == -1)
   {
    //EOS
   }
   else
   {
    //. is found

    SelectCSNumbers(tmpRange.duplicate());
   }
  }
  else
  {

   SelectCSNumbers(tmpRange.duplicate());
  }
 }
phsr
+1  A: 

Random guess:

if (tmpRange.text.Length == myRange.text.Length)
{
    count++;
    return tmpRange.duplicate();
}

If you put count++ after the return statement, it will never be executed.

dtb