c# - Re-defne a Paragraph Range to Include a Subset of Words in the Paragraph -
c# - Re-defne a Paragraph Range to Include a Subset of Words in the Paragraph -
i'm using microsoft.interop.word / c# programmatically create word document. in given paragraph, need alter color of words occur between brackets "[" , "]"
the words variable.
as understand it, need range object in order set colors. i'm using next code, not work:
// add together paragraph. microsoft.office.interop.word.paragraph pgf = document.paragraphs.add(); // insert text paragraph. pgf.range.insertbefore("jira issue: " + jiraissueid + " [" + statusname + "]"); //get start , end locations of brackets. //these define location of words apply colors. int startpos = pgf.range.text.indexof("[") + 1; int endpos = pgf.range.text.indexof("]") - 1; /* effort alter range start , end positions, range contains words between brackets. i've tried 2 methods. */ // next line not work. pgf.range.setrange(startpos, endpos); // , next lines not work either pgf.range.start = startpos; pgf.range.end = endpos; // effort alter color. next line changes color of entire paragraph: pgf.range.font.color = microsoft.office.interop.word.wdcolor.wdcolorred; /* next prints entire contents of paragraph, , not words between brackets. attempts @ narrowing range not successful. */ console.writeline(pgf.range.text);
your help appreciated.
the setrange doesn't work because can't alter origin or ending of paragraph. begins begins. need "own" range can modify. luck there exists property duplicate of range. msdn says: by duplicating range object, can alter starting or ending character position of duplicate range without changing original range.
first assume ms word namspace included:
using microsoft.office.interop.word;
so after calculating startpos , endpos should alter code (updated):
microsoft.office.interop.word.range rng = pgf.range.duplicate; rng.moveend(wdunits.wdcharacter, endpos - rng.characters.count); rng.movestart(wdunits.wdcharacter, startpos); rng.font.color = microsoft.office.interop.word.wdcolor.wdcolorred;
and should work. (just don't decrement endpos lines above; realize it)
c# .net ms-word ms-office office-interop
Comments
Post a Comment