簡體   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