VSTO comment RCW points to the wrong comment

42 views Asked by At

I've come across a very strange behavior in Word VSTO. I don't know why it is happening or how to avoid it. I can demonstrate it with the following code. If _comment is set, then show its content in a MessageBox. If _comment is not set, then set it to the first comment in the document.

    private void Application_DocumentBeforeSave(Word.Document Doc, ref bool SaveAsUI, ref bool Cancel)
    {
        if(_comment != null)
        {
            MessageBox.Show(_comment.Range.Text);
        }
        else
        {
            var comments = Application.ActiveDocument.Range().Comments;
            if (comments.Count > 0)
            {
                _comment = comments[1];
            }
        }
    }

The strange behavior happens when the user adds a second comment with the exact same scope as the first. When that happens, _comment refers to the second comment instead of the first.

_comment is set to some comment: _comment is set

The user adds a second comment with the same scope as the first (when nothing is highlighted, the word the cursor is in will be selected) Second comment is inserted inside scope of first comment, without highlighting anything.

_comment now refers to the second comment, for some reason: Second comment

_comment now points to the second comment for some reason.

Can anyone explain what is happening here? Am I missing something, or is this a bug in the interop?

0

There are 0 answers