How to remove Word.Selection?

In the context of an Outlook add-in using WordEditor from Outlook.Inspector, I would like to remove this selection after replacing it with custom text.

For example, if I select something, I can change the selection to 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 will be 1 instead of 0.

+4
source share
2 answers

I had the same problem in MS Word. Word has a method called Selection.Move (). If you use it, it will deselect the text and place the cursor at the end of the selected text. For example, you can use

 ThisAddIn.Application.Selection.Move() 

This works in Word add in, if you want to deselect the selected text, it can also work in Outlook, try and let me know

+4
source

You might want to try

 Selection.Collapse Direction := wdCollapseStart 

This will set the start and end positions of the current selection to the same value, namely the beginning of the current selection (instead of wdCollapseEnd to end the current selection). The result should be programmatically indistinguishable from the actual deletion of the selection.

hope this helps, carsten

applies to: word 2007 (verified), word 2010; possibly other releases

+1
source

All Articles