【问题标题】:Fix a text box (at the right) in PowerPoint slide from Excel VBA从 Excel VBA 修复 PowerPoint 幻灯片中的文本框(右侧)
【发布时间】:2019-02-02 19:49:05
【问题描述】:

我使用以下代码将一个单元格的内容放在 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

我希望框中的文本与右上角的文本框对齐。像这样:

因为文字是可以改变的,所以长度和我只能改变这些参数(Left, Top, Width & Height),我有这个:

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

【问题讨论】:

    标签: excel vba powerpoint


    【解决方案1】:

    你已经很接近了,我们只需要修改一些代码。

    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
    

    【讨论】:

    • 谢谢你的回答,它帮助我在右边对齐,但我想把右边的盒子修好。因为句子的长度发生了变化,我想把句子的结尾固定在右边。
    • 我将更改代码以使其与右侧对齐,我将展示我们如何实现的两种方法,然后您可以选择最适合您的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    • 1970-01-01
    相关资源
    最近更新 更多