I'm using C# over VS2022, to write a VSTO add-in for Word, intended to auto-style in-line text-portions of a certain pre-defined format, e.g. to apply a (pre-defined) style to any text in brackets.
To exemplify the desired result, notice how a special (italic style) is applied to bracketed portions of the following text:
This (part in brackets) should demonstrate the (desired) result.
In code, after a target-portion is found, it is wrapped by a Range
object, like so:
Range oRange = doc.Range(iStartIndex, iEndIndex);
This works well - range encapsulates exactly the required sequence of characters.
But, when calling oRange.set_Style (styleName)
, style is applied to the entire line in which this range lies.
Behavior is the same:
set_Style
method is used like so: object szStyle="Heading 1"; oRange.set_Style (ref szStyle);
set_Style
method is used like so: oRange.set_Style (doc.Styles[styleName]);
I've found a close, yet-unanswered question here.
It seems that the issue was caused due to applied style's type.
When it was anything other than Character, style was applied to the entire line.
Only Character-type styles could be successfully applied to an in-line Range
.