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

感情迁移 提交于 2020-01-06 06:10:21

问题


I put one cell's content on a PowerPoint slide with this code:

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

I want that the text in the box be aligned to the right and the text box in the right top corner. Like this:

Because the text can change, so as the length and I can only change these parameters (Left, Top, Width & Height), I have this:

How to set the text box to fix it in the right top corner, and how to align the text at right?


回答1:


You are pretty close, we just need to modify some of the code.

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

All I did was add the code that will align the content of the TextRange to the right:

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

Now, to actually align the text box itself to the upper right corner of the slide I'm going to add the text box to a shape range. From here, I'm going to use the built-in align method to align it to the top of the slide and the far right corner.

'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


来源:https://stackoverflow.com/questions/53885841/fix-a-text-box-at-the-right-in-powerpoint-slide-from-excel-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!