以编程方式在单元格中居中文本:适用于单行文本,但不会持续更长时间

Fre*_*low 2 c# powerpoint office-interop

我在 PowerPoint 中创建了一个表格,我想将每个单元格中的文本水平居中。我使用了MsoHorizo​​ntalAnchor.msoAnchorCenter

如果文本很短且在一行中,则效果很好,但如果文本很长,则显示在 1 行以上且不居中。编码:

myShape.Table.Cell(1, 1).Shape.TextFrame.TextRange.Text = "shortOK";
myShape.Table.Cell(1, 2).Shape.TextFrame.TextRange.Text = "Long text is not displayed centered";

myShape.Table.Cell(1, 1).Shape.TextFrame.VerticalAnchor = Microsoft.Office.Core.MsoVerticalAnchor.msoAnchorMiddle;                 
myShape.Table.Cell(1, 2).Shape.TextFrame.VerticalAnchor = Microsoft.Office.Core.MsoVerticalAnchor.msoAnchorMiddle;

myShape.Table.Cell(1, 1).Shape.TextFrame.HorizontalAnchor = Microsoft.Office.Core.MsoHorizontalAnchor.msoAnchorCenter;
myShape.Table.Cell(1, 2).Shape.TextFrame.HorizontalAnchor = Microsoft.Office.Core.MsoHorizontalAnchor.msoAnchorCenter;
Run Code Online (Sandbox Code Playgroud)

如果显示在 1 法以内,您知道如何使文本居中吗?

小智 6

我会使用这个文档,“ https://docs.microsoft.com/en-us/office/vba/api/powerpoint.paragraphformat.alignment ”。互操作中的 TextRange 有一个叫做“PPParagraphAlignment.ppAlignCenter”的东西,例如:

 textrange.ParagraphFormat.Alignment = PpParagraphAlignment.ppAlignCenter;
Run Code Online (Sandbox Code Playgroud)