Why text is appended when writing text to bookmarks in a doc?

 

bookmark.Range.Text = "A";

bookmark.Range.Text = "B";

the result in doc is "AB"!

 

 



Answer this question

Why text is appended when writing text to bookmarks in a doc?

  • Chris.NET

    Hi,

    It sounds like for your test case, you have created a bookmark that contains a range that has no characters in it.  In other words, your bookmark marks an insertion point, not a selection.  In the case where the range object contains no characters, setting the text property will result in an insertion.  I suspect that for your results you are really seeing BA, not AB.

    That being said, is this a VSTO question   If you are using a VSTO project, you should use the Microsoft.Office.Tools.Word.Bookmark class and set the text property directly on it.  The reason for this is that if you set the text property on a Word bookmark's Range  where end > start, the bookmark will be deleted.  The Bookmark class that VSTO provides will work around this behavior for you automatically--provided you set the Text properly directly and don't go through the Range object.

    If this was not a VSTO question, it should have been posted in the Word Vba forum: http://msdn.microsoft.com/newsgroups/default.aspx dg=microsoft.public.word.vba.general&lang=en&cr=US

    Sincerely,

    Geoff Darst

    Microsoft VSTO Tools


  • Rajni Roopchandani

    It is a VSTO question,and the result is BA.

    thx for ur ans. 


  • Why text is appended when writing text to bookmarks in a doc?