Search code examples
vbapowerpoint

"Text Box Vertical Alignment" to "Middle" for a selected rows in PowerPoint through vba macro


enter image description hereI would like to create a macro which will change the "Vertical Alignment" to "Middle" of the selected rows/cells in a PowerPoint table. Can anyone pls help me with this.

Below example snapshot attached.

Below is the code. My code is perfectly working with the shape but could't work for the tables. pls assist.

ActiveWindow.Selection.ShapeRange.TextFrame2.VerticalAnchor = msoAnchorMiddle

Solution

  • To approach a table you can not use the Shape Object, but need to use Tables. You can also format only Cell by Cell so you need to run a loop through all Rows and Columns

    -----edited-----

    To use the selected cells you have to iterate through all cells and see if they are selected

    Sub SelectedCells()
    Dim oTbl As table
    Dim iIdx As Integer
    Dim iIdy As Integer
    Set oTbl = ActiveWindow.Selection.ShapeRange(1).table
    For iIdx = 1 To oTbl.Rows.Count
        For iIdy = 1 To oTbl.Columns.Count
            If oTbl.Cell(iIdx, iIdy).Selected Then
                With oTbl.Cell(iIdx, iIdy).Shape
                .TextFrame.VerticalAnchor = msoAnchorTop
                End With
            End If
        Next
    Next
    End Sub