2012-07-18 71 views
3

我正在使用Powerpoint 2007,我想编写一个宏,它使幻灯片中的文本框。VBA PowerPoint文本框alignement

但是,默认情况下,文本框中的文本与中心对齐。 我想将它左对齐,但我不知道该怎么做。 如何更改此文本框的对齐方式?

这是我的代码。

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 

回答

4

首先,在代码中任何选择,也不依赖于当前的选择通常不是好的做法如果仅仅是因为它可以通过数量级放慢你的代码。

取而代之的是,这样的事情:

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 
1

您的问题的答案,我相信是在Shape.TextFrame.TextRange对象属性

oPPShape.TextFrame.TextRange.ParagraphFormat.Alignment = msoAlignLeft 

只是一个惊人之语你和史蒂夫的帖子。如果您真的使用此代码和对象进行后期绑定,则还需要记住从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") 

More of late binding here.