2012-07-18 103 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.