3

I have this text in the document:"50%"; when I run this function it just returns "50" and after that it returns "%". I dont know why it is spliting the 50 of the %... Can you please tell me how can I avoid this behavior in order to get the complete word "50%", instead "50" and "%"?

int astart = 0;
int aend = Doc.Content.End;

//docwords.Words = '50%'
Range docwords = Doc.Range(ref astart, ref aend);

foreach (Range word in docwords.Words)
{
    // here first return "50" and after return "%"
    String wordText = word.Text;
}
4
  • 1
    Missing context, and very poor explanation. You should give some more and explain the question better. Moreover ignorechars is never used. Jun 15, 2011 at 23:15
  • You never use ignorechars... Jun 15, 2011 at 23:17
  • Its in the definition of a word. What does Doc define a word as? It appears that non-alphanumeric characters are considered their own words. Also, the ignorechars does nothing. Jun 15, 2011 at 23:17
  • Start Word, type "50%", use Ctrl+Shift+right arrow to select a word. Yup, two words. Jun 15, 2011 at 23:30

1 Answer 1

1

I'm presuming you're using Office10 and the Word API. Based on this @Richard is correct. Words are broken by punctuation, a space, or being at the start or end of a line.

If you want to avoid the split you may be better off selecting your words using a RegEx and Matches collection. Something like Regex.Matches(Document.Text, @"[A-Za-z0-9]+") may help. (And stick the punctuation that you want into the square brackets.

1
  • How would he write this code with RegEx expressions instead of iterating through docwords.Words?
    – Jason
    Jun 16, 2011 at 1:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.