繁体   English   中英

从Excel VBA修复PowerPoint幻灯片中的文本框(在右侧)

[英]Fix a text box (at the right) in PowerPoint slide from Excel VBA

我使用以下代码将一个单元格的内容放在PowerPoint幻灯片上:

Set Sh = Pres.Slides(1).Shapes.AddLabel(Orientation:=msoTextOrientationHorizontal, _
    Left:=80, Top:=58, Width:=150, Height:=45)
Sh.TextFrame.TextRange.Text = Worksheets("Image ppt").Range("C38").Value 
Sh.TextFrame.TextRange.Font.Color = RGB(0, 75, 125)
Sh.TextFrame.TextRange.Font.Size = 16
Sh.TextFrame.TextRange.Font.Bold = True

我希望框内的文本与右边对齐,而框在右上角。 像这样:

在此处输入图片说明

因为文本可以更改,所以长度和我只能更改这些参数(左,上,宽度和高度),因此我需要:

在此处输入图片说明

如何设置文本框以将其固定在右上角,以及如何将文本右对齐?

您非常接近,我们只需要修改一些代码即可。

Set Sh = Pres.Slides(1).Shapes.AddLabel(Orientation:=msoTextOrientationHorizontal, _
        Left:=80, Top:=58, Width:=150, Height:=45)
    Sh.TextFrame.TextRange.Text = Worksheets("Image ppt").Range("C38").Value 
    Sh.TextFrame.TextRange.Font.Color = RGB(0, 75, 125)
    Sh.TextFrame.TextRange.Font.Size = 16
    Sh.TextFrame.TextRange.Font.Bold = True

    'Add this to align the content of the textbox to the right.
    Sh.TextFrame.TextRange.ParagraphFormat.Alignment = ppAlignRight

'Select the Shape.
Sh.Select

'With the active selection, align it to the upper right corner.
With Application.ActiveWindow.Selection.ShapeRange

     'If you want it exactly in the upper right corner use this.
     .Align msoAlignRights, msoTrue
     .Align msoAlignTops, msoTrue

     'If you want a little space between the slide & the text box, this is 
     'the approximate value for the upper right corner.
     .Left = 870
     .Top = 10

End With

我所做的就是添加将TextRange的内容向右对齐的代码:

'Add this to align the content of the textbox to the right.
 Sh.TextFrame.TextRange.ParagraphFormat.Alignment = ppAlignRight

现在,要将文本框本身实际对齐到幻灯片的右上角,我将文本框添加到形状范围。 从这里开始,我将使用内置的align方法将其与幻灯片的顶部和最右角对齐。

'Select the Shape.
Sh.Select

'With the active selection, align it to the upper right corner.
With Application.ActiveWindow.Selection.ShapeRange

     'If you want it exactly in the upper right corner use this.
     .Align msoAlignRights, msoTrue
     .Align msoAlignTops, msoTrue

     'If you want a little space between the slide & the text box, this is 
     'the approximate value for the upper right corner.
     .Left = 870
     .Top = 10

End With

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM