从Excel VBA修复PowerPoint幻灯片中的文本框(在右侧) [英] Fix a text box (at the right) in PowerPoint slide from Excel VBA

查看:81
本文介绍了从Excel VBA修复PowerPoint幻灯片中的文本框(在右侧)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

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

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

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

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

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

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

这篇关于从Excel VBA修复PowerPoint幻灯片中的文本框(在右侧)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆