PDA

View Full Version : Change paddings using VBA



Platypuce
07-03-2019, 08:29 AM
Hello,
I’m new to VBA, and I’m working on Powerpoint 2016. I’m looking to change the properties of a "Shape" object with VBA.
I have a single Slide with a single Shape object,which is a table (or list) of 4 columns over 10 rows, with text in each of the cells (I don’t know if in PowerPoint, the term "cell" is adapted).
I manage to change properties of the Shape object such as Left, Top, Height, Width, but now I have to change the internal margins (or padding) in my table cells.
I guess I have to act on the "TextFrame" object and the MarginLeft property for example.
The following code is used:


Dim myShape As Shape
ActivePresentation.Slides(1).Select
Set myShape = ActivePresentation.Slides(1).Shapes(1)
myShape.TextFrame.MarginLeft = 5

and does not work, and an error message "The specified value is out of range" appears.
Can you help me understand my mistake?

Note: I have tested the property " HasTextFrame " and the message box is displaying "0", which I guess means that the TextFrame is NULL.
But why, as the Shape contains text?

Thanks !
Platypuce

John Wilson
07-04-2019, 05:34 AM
Tables do not have a textframe. They contain cells which DO.

You need to loop through each cell like this:


Sub int_margins()
Dim otbl As Table
Dim iRow As Integer
Dim iCol As Integer
On Error Resume Next
Set otbl = ActiveWindow.Selection.ShapeRange(1).Table
If Not otbl Is Nothing Then
For iRow = 1 To otbl.Rows.Count
For iCol = 1 To otbl.Columns.Count
With otbl.Cell(iRow, iCol).Shape.TextFrame
.MarginTop = 0
.MarginBottom = 0
.MarginLeft = 0
.MarginRight = 0
End With
Next iCol
Next iRow
End If
End Sub

Platypuce
07-05-2019, 06:41 AM
Hi John Wilson,
Thank you for your reply.
This is what I was searching for, and will help me a lot.
Platypuce.