英文:
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
- 你可以使用
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;
- 在使用
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:
- You can, work with the
InsertAfter
(andInsertBefore
) 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;
- Create a second
TextRange
object when usingInsertAfter
. 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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论