none
Range.MoveStart Word.WdUnits.wdWord with email RRS feed

  • Question

  • I notice that while typing my email (sanjilyh@hotmail.com) in Microsoft Word, it treats the email as 1 word. But when I used the following code (where my cursor is infront of the email):

    Word.Range range = this.Application.Selection.Range;
    range.MoveStart(Word.WdUnits.wdWord, -1);

    And i used the following code to retrieve the text:

    string temp = range.Text;

    The output is "com". It treated my email as a combination of a few words.

    Is there anyway I can configure to allow Word.WdUnits.wdWord to recognize email as one word? Thank you.

    Tuesday, June 12, 2012 4:46 PM

Answers

  • That's an interesting observation. Maybe when it's 'looking' for the end of the word, finding the @ character makes it disregard the .

    A way around it would be to use the MoveStartUntil method. You can enter the number of characters, as a negative number, if you want it to look only a certain distance for the start of the email address, or wdBackward to search indefinitely.

    The below example will move the range start until it finds a space character, a carriage return, or give up after it has searched for 100 characters.

    string characterSet
    characterSet[0] = ' ';
    characterSet[1] = 13;
    
    int maxCharactersToMove = -100;
    
    range.MoveStartUntil(characterSet, maxCharactersToMove);


    Tuesday, June 12, 2012 8:14 PM
  • "Is there anyway I can configure to allow Word.WdUnits.wdWord to recognize email as one word? "

    No.  The range object uses wdUnits that are delimited by periods (among others), so sanjilyh@hotmail.com is - in fact - NOT one word. At least when dealing with a range object.  As a range object it is FIVE words.

    sanjilyh

    @

    hotmail

    .

    com


    Word MVP



    Saturday, June 23, 2012 5:22 AM

All replies

  • That's an interesting observation. Maybe when it's 'looking' for the end of the word, finding the @ character makes it disregard the .

    A way around it would be to use the MoveStartUntil method. You can enter the number of characters, as a negative number, if you want it to look only a certain distance for the start of the email address, or wdBackward to search indefinitely.

    The below example will move the range start until it finds a space character, a carriage return, or give up after it has searched for 100 characters.

    string characterSet
    characterSet[0] = ' ';
    characterSet[1] = 13;
    
    int maxCharactersToMove = -100;
    
    range.MoveStartUntil(characterSet, maxCharactersToMove);


    Tuesday, June 12, 2012 8:14 PM
  • "Is there anyway I can configure to allow Word.WdUnits.wdWord to recognize email as one word? "

    No.  The range object uses wdUnits that are delimited by periods (among others), so sanjilyh@hotmail.com is - in fact - NOT one word. At least when dealing with a range object.  As a range object it is FIVE words.

    sanjilyh

    @

    hotmail

    .

    com


    Word MVP



    Saturday, June 23, 2012 5:22 AM