Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA Powerpoint Textbox 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
like image 362
user1534778 Avatar asked Jan 17 '23 01:01

user1534778


2 Answers

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
like image 73
Steve Rindsberg Avatar answered Jan 22 '23 07:01

Steve Rindsberg


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.

like image 41
tobias Avatar answered Jan 22 '23 09:01

tobias