英文:
VSTO: MS-Word Range-Styling Affects Entire Line
问题
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:
- for various languages and font-types.
- even if the
set_Style
method is used like so:object szStyle="Heading 1"; oRange.set_Style (ref szStyle);
- even if the
set_Style
method is used like so:oRange.set_Style (doc.Styles[styleName]);
I've found a close, yet-unanswered question here.
英文:
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:
- for various languages and font-types.
- even if the
set_Style
method is used like so:object szStyle="Heading 1"; oRange.set_Style (ref szStyle);
- even if the
set_Style
method is used like so:oRange.set_Style (doc.Styles[styleName]);
I've found a close, yet-unanswered question here.
答案1
得分: 0
似乎问题是由于应用的样式类型引起的。
当样式类型不是“字符”时,样式会应用于整行。
只有“字符”类型的样式才能成功应用于内联的 Range
。
英文:
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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论