PDA

View Full Version : [SOLVED:] VBA for align text in shape or selected table cells



RayKay
03-31-2020, 08:58 AM
Hi John, I've tried about 2 hours and several different version of code for something so simple (not for me though!).

I need code please that aligns table text in PowerPoint for selected cells. That also works in for a text box. I know there's tools and button already, but making a ribbon I can't insert the msoNAME for these tools. I need a tool to align text to the top. (I can then guestimate for middle and bottom).

Code I ended up close to it is:


Sub AlignTop()


Dim tbl As Table
Dim iCol As Integer
Dim iRow As Integer
Dim I As Integer
On Error GoTo err


Dim X As Integer
Dim Y As Integer
Dim otbl As Table
Dim B As Long
On Error GoTo err:
Set otbl = ActiveWindow.Selection.ShapeRange(1).Table
otbl.Parent.Height = 0
For X = 1 To otbl.Columns.Count
For Y = 1 To otbl.Rows.Count
With otbl.Cell(Y, X)
.Shape.TextFrame2.VerticalAnchor = msoAnchorTop
End With


End Sub




Your wisdom is much appreciated, thank you. And take care in these strange times.
Thank you.

John Wilson
04-01-2020, 06:48 AM
Maybe


Sub AlignTop()

Dim iCol As Integer
Dim iRow As Integer
Dim otbl As Table
On Error GoTo Err


Set otbl = ActiveWindow.Selection.ShapeRange(1).Table
otbl.Parent.Height = 0
For iRow = 1 To otbl.Rows.Count
For iCol = 1 To otbl.Columns.Count


With otbl.Cell(iRow, iCol)
.Shape.TextFrame2.VerticalAnchor = msoAnchorTop
End With
Next iCol
Next iRow
Exit Sub
Err:
MsgBox "There was an error: " & Err.Description, vbCritical
End Sub