VBA PowerPoint文本框文本对齐方式



我使用的是PowerPoint 2007,我想编写一个宏,在幻灯片中创建一个文本框。

但是,默认情况下,文本框中的文本将居中对齐。

我想把它向左对齐,但我不知道该怎么做。如何更改此文本框的对齐方式?

这是我的密码。

Set objPPT = CreateObject("PowerPoint.Application")
Set SQ = objPPT.Presentation
......
SQ.Slides(i + 6).Shapes.AddTextbox(msoTextOrientationHorizontal, 50, 100, 600, 100).Select
objPPT.ActiveWindow.Selection.TextRange.Text = titre

首先,选择代码中的任何内容或依赖当前的选择通常都不是一种好的做法,因为这会使代码慢几个数量级。

相反,像这样的东西:

Dim oSh as Object ' if you're using late binding, as Shape if early binding
Set oSh =  SQ.Slides(i + 6).Shapes.AddTextbox(msoTextOrientationHorizontal, 50, 100, 600, 100)
' notice that I've removed the .Select from the end of the line above
With oSh.TextFrame
  .TextRange.Text = "something"
  .TextRange.Paragraphs.ParagraphFormat.Alignment = ppAlignLeft
End With

我认为问题的答案是Shape.TextFrame.TextRange对象属性

oPPShape.TextFrame.TextRange.ParagraphFormat.Alignment = msoAlignLeft

只是对Your和Steve的帖子的评论。如果你真的在使用这些代码和对象进行后期绑定,你还需要记住定义PowerPoint库中的常量,比如msoTextOrientationHorizontal。当你从项目中删除PPT引用时,你会很快发现哪些常量被遗漏了。与Excel一样,将宏分发给不同版本的用户最好使用后期绑定,因为Office产品引用与版本无关。

'Bind to an existing or created instance of Microsoft Powerpoint
Dim objPPTApp As Object
On Error Resume Next
Set objPPTApp = GetObject(, "Powerpoint.Application")
If Err.Number <> 0 Then: Err.Clear: Set objPPTApp = CreateObject("Powerpoint.Application")

这里有更多的后期绑定。

最新更新