How to remove a Word.Selection?
In the context of an Outlook add-on using the WordEditor from the Outlook.Inspector I would like to remove the selection after replacing it with a custom text.
For example, if I select something I can change the selection to a custom text as follows:
Word.Selection sel = doc.Windows[1].Selection;
Word.Range range = sel.Range;
if(sel.Text.Length == 0) {
开发者_开发技巧 MessageBox.Show("No Text is selected");
return;
}
sel.Text = "New Text";
sel.Collapse();
If I call this function again, now sel.Text.Length is equal to 1 instead of 0.
I had the same problem in MS Word. Word has a method called Selection.Move(). If you use it it will deselect the selected text and place the cursor at the end of the selected text. For example you can use
ThisAddIn.Application.Selection.Move()
This works in a Word add in if you want to deselect selected text, it may work in Outlook too, try and let me know
You might wish to try
Selection.Collapse Direction := wdCollapseStart
This will set the start and end positions of the current selection to the same value, namely the start of the current selection (specify wdCollapseEnd
instead for the end of the current selection). The result should be programmatically indistinguishable from actually deleting a selection.
hope this helps, carsten
applies to: word 2007 (tested), word 2010; possibly other releases
精彩评论