C# PowerPoint VSTO Addin:在 PowerPoint TextRange 中添加文本后更改上标字体样式

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

C# PowerPoint VSTO Addin: Change superscript font style after added text in PowerPoint TextRange

问题

在我的一个C# PowerPoint VSTO插件中,我在当前光标位置的形状TextRange中添加了一些上标文本,像这样:

PowerPoint.TextRange superscriptText = Globals.ThisAddIn.Application.ActiveWindow.Selection.TextRange.InsertAfter("xyz");
superscriptText.Font.Superscript = Office.MsoTriState.msoCTrue;

这个代码按预期工作:字符串"xyz"以上标形式插入到当前光标位置。问题在于,一旦插入了"xyz",字体样式仍然保持为上标,即用户在插入"xyz"后在光标位置输入的文本仍然是上标的。我如何在插入上标文本后将光标位置的文本样式更改回非上标?我尝试过以下方法但没有成功:

Globals.ThisAddIn.Application.ActiveWindow.Selection.TextRange.Font.Superscript = Office.MsoTriState.msoFalse;

但进一步输入的文本仍然保持上标样式。

英文:

In one of my C# PowerPoint VSTO Addins, I have added some text in superscript at the current cursor location in a shape's TextRange like this

PowerPoint.TextRange superscriptText = Globals.ThisAddIn.Application.ActiveWindow.Selection.TextRange.InsertAfter("xyz");
superscriptText.Font.Superscript = Office.MsoTriState.msoCTrue;

This works as intended: the string "xyz" is inserted in superscript at the current cursor location. The problem is that once "xyz" is inserted, the font style is still in superscript for all text that follows i.e., text that the user types at the cursor after inserting the "xyz". How can I change the tex style at the cursor back to being non-superscripted after inserting superscripted text? I have unsuccessfully tried with

Globals.ThisAddIn.Application.ActiveWindow.Selection.TextRange.Font.Superscript = Office.MsoTriState.msoFalse;

But further typed text still continues in superscript.

答案1

得分: 1

  1. 你可以使用 InsertAfter(和 InsertBefore)方法与所需的格式结合使用。

格式会附加到 InsertAfter 方法

PowerPoint.TextRange superscriptText = Globals.ThisAddIn.Application.ActiveWindow.Selection.TextRange;
superscriptText.InsertAfter("xyz").Font.Superscript = Office.MsoTriState.msoCTrue;
superscriptText.InsertAfter(" not superscript").Font.Superscript = Office.MsoTriState.msoCFalse;
  1. 在使用 InsertAfter 时,创建第二个 TextRange 对象。语言参考中说明:

> 将字符串附加到指定文本范围的末尾。返回表示附加文本的 TextRange 对象。

创建一个新的 TextRange 并单独格式化

PowerPoint.TextRange superscriptText = Globals.ThisAddIn.Application.ActiveWindow.Selection.TextRange;
superscriptText.InsertAfter("xyz");
superscriptText.Font.Superscript = Office.MsoTriState.msoCTrue;
PowerPoint.TextRange normalText = superscriptText.InsertAfter(" not superscript");
normalText.Font.Superscript = Office.MsoTriState.msoCFalse;
英文:

Two possibilities:

  1. You can, work with the InsertAfter (and InsertBefore) methods in combination with the required formatting.

The formatting is appended to the InsertAfter method:

PowerPoint.TextRange superscriptText = Globals.ThisAddIn.Application.ActiveWindow.Selection.TextRange;
superscriptText.InsertAfter("xyz").Font.Superscript = Office.MsoTriState.msoCTrue;
superscriptText.InsertAfter(" not superscript").Font.Superscript = Office.MsoTriState.msoCFalse;
  1. Create a second TextRange object when using InsertAfter. The language reference states:

> Appends a string to the end of the specified text range. Returns a
> TextRange object that represents the appended text.

Create a new TextRange and format it separately

PowerPoint.TextRange superscriptText = Globals.ThisAddIn.Application.ActiveWindow.Selection.TextRange;
superscriptText.InsertAfter("xyz")
superscriptText.Font.Superscript = Office.MsoTriState.msoCTrue;
PowerPoint.TextRange normalText = superscriptText.InsertAfter(" not superscript")
normalText.Font.Superscript = Office.MsoTriState.msoCFalse;

huangapple
  • 本文由 发表于 2020年1月3日 17:55:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576408.html
匿名

发表评论

匿名网友

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

确定