none
Strange behavior of Range object during text edit RRS feed

  • Question

  • I encoutered strange behavior in persisted Word.Range objects. For test purposes I saved first sentence range in static variable

    static Word.Range _tmp = null;

    For example I have a windows form and timer (WinForms timer) which check _tmp and post debug message like this:

    StringBuilder sb = new StringBuilder();

    if (_tmp.Text.Length < _tmp.End)

    {

        sb.Append(_tmp.End);

        sb.Append(" > ");

        sb.AppendLine(_tmp.Text.Length.ToString());

     

        sb.AppendLine(_tmp.Text);

        Word.Characters chars = _tmp.Characters;

        for (int i = 1; i <= chars.Count; i++)

        {

            sb.Append(chars[i].Start);

            sb.Append(",");

        }

        sb.AppendLine();

     

     

        XUtilities.Release(chars);

        sb.AppendLine(_doc.Sentences.First.End.ToString());

     

        XCore.LogMessage(sb.ToString());                   

    }

    else XCore.LogMessage("ok");

    If i made any changes in sentence (add 2-3 chars in front or in the middle) - End becomes much greater than text length (+100 or something like that). And there is hole between Word chars ranges where text is edited. Debug message shows 1,2,3,103,104,105 etc.

    BUT! If main pane of Word loses focus (for example, I click title of form) - everething becomes ok and Range end becomes equal to length. Also everething becomes ok if I select some text in word manually or programmatically.

    Is this some kind of edit mode? How can I detect this?

    This is COM Add-in (without VSTO - just COM-shimmed addin) in .net 2.0. 

    Word 2010 32bit SP1

    VS 2010 SP1


    Беликов Сергей Программист-фрилансер MCTS
    Tuesday, July 26, 2011 11:22 AM

Answers

  • I don't know the language so I may not help, but you have ..
     
        if (_tmp.Text.Length < _tmp.End) { ...
     
    .Length is a .NET thing that (I'm guessing) gives you the length of the String that the .Text property returns. .End is a property of the Range object that reflects the end position of the range in the document and the only time it may not be greater than the text length is when the Range is the complete Document, so I don't understand what the check is supposed to be doing.
     

    Enjoy,
    Tony
    www.WordArticles.com
    Wednesday, July 27, 2011 10:27 AM

All replies

  • Find out that calling Calculate hepls (even for another range), but I can't use it because of performance issues. If i call calculate each second, cursor quickli switch to clocks and back. Very annoing...

    So Calculate does something, that updates ranges. But what? Is there any methods that I can use instead of calling Calculate each time before I get start and end?


    Беликов Сергей Программист-фрилансер MCTS
    Wednesday, July 27, 2011 8:34 AM
  • I don't know the language so I may not help, but you have ..
     
        if (_tmp.Text.Length < _tmp.End) { ...
     
    .Length is a .NET thing that (I'm guessing) gives you the length of the String that the .Text property returns. .End is a property of the Range object that reflects the end position of the range in the document and the only time it may not be greater than the text length is when the Range is the complete Document, so I don't understand what the check is supposed to be doing.
     

    Enjoy,
    Tony
    www.WordArticles.com
    Wednesday, July 27, 2011 10:27 AM