Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 932
Baadal Gupta Avatar asked Jul 18 '11 17:07

Baadal Gupta


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 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

like image 60
Hüseyin DEMİRTAŞ Avatar answered Sep 27 '22 17:09

Hüseyin DEMİRTAŞ


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

like image 40
collapsar Avatar answered Sep 27 '22 17:09

collapsar