user1534778
user1534778

Reputation: 41

VBA PowerPoint textbox text alignment

I am using PowerPoint 2007 and I want to program a macro which creates a textbox in a slide.

However the text in the textbox is aligned to center by default.

I want to align it left, but I don't know how to do. How can I change alignment of this textbox?

Here is my code.

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

Upvotes: 4

Views: 28719

Answers (2)

tobias
tobias

Reputation: 61

The answer to your problem I believe is in Shape.TextFrame.TextRange object properties

oPPShape.TextFrame.TextRange.ParagraphFormat.Alignment = msoAlignLeft

Just a remark to Your and Steve's post. If you're really using this code and objects for late binding, you also need to remember to define the constants from PowerPoint library like msoTextOrientationHorizontal. You'll quickly find when you remove the PPT reference from your project which constants are left out. Like with Excel, distributing your macro to users with different versions is best done with late binding where Office product references are version independent.

'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.

Upvotes: 3

Steve Rindsberg
Steve Rindsberg

Reputation: 96

First, selecting anything in code or relying on the current selection is generally not good practice if only because it can slow your code down by orders of magnitude.

Instead, something like this:

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

Upvotes: 7

Related Questions