VSTO: MS-Word范围样式影响整行

huangapple go评论62阅读模式
英文:

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:

  1. for various languages and font-types.
  2. even if the set_Style method is used like so: object szStyle="Heading 1"; oRange.set_Style (ref szStyle);
  3. 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:

  1. for various languages and font-types.
  2. even if the set_Style method is used like so: object szStyle="Heading 1"; oRange.set_Style (ref szStyle);
  3. 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.

huangapple
  • 本文由 发表于 2023年6月26日 19:25:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76556223.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定