Resize-to-fit Table Columns (PowerPoint)


22,707
0
Filed under -
Forum ,

Resize-to-fit Table Columns (PowerPoint)

Resize-to-fit Table Columns (PowerPoint)

I have a table in PowerPoint with several rows and columns of data. I would like to resize selected columns so the width of the column does not exceed the width of the longest data – in otherwords make the column width the perfect fit. I am wondering how I would go abouts in doing this?

Below are my various attempts but they have failed as when I went to run my macro, the column widths did not change.

[code]mySlide.Shapes(mySlide.Shapes.Count).Table.Cell(x, y).Shape.TextFrame.WordWrap = msoFalse
mySlide.Shapes(mySlide.Shapes.Count).Table.Cell(x, y).Shape.TextFrame.AutoSize = ppAutoSizeShapeToFitText[/code]

[code]mySlide.Shapes(mySlide.Shapes.Count).Table.Columns (x).Width = ppAutoSizeShapeToFitText[/code]

[code]mySlide.Shapes(mySlide.Shapes.Count).Table.Cell(ta rgetRow, targetColumn).Shape.TextFrame.AutoSize = ppAutoSizeShapeToFitText[/code]

My solution!

Not sure if this thread is still active?

I had much the same challenge as icu222much – wanting to use vba to automate the resizing of tables to match cell contents. My solution was to create (programmatically) a text box, put the text of the cell into the text box (together with font size and font name), resize the text box, get width and height of the text box, and use these values to set width and height of the cell in the table. Once all the cells are done, the text box is deleted.

Had thought of posting the code here, but it exceeds the limit on number of characters Is there a way to post vba code to this forum?

6

Sub table_fix()
Dim icol As Integer, irow As Integer, minW As Single
With ActiveWindow.Selection.ShapeRange(1).Table
For icol = 1 To .Columns.Count
For irow = 1 To .Rows.Count
With .Cell(irow, icol).Shape.TextFrame
If minW = 0 Then minW = .TextRange.BoundWidth + .MarginLeft + .MarginRight + 1
If minW < .TextRange.BoundWidth + .MarginLeft + .MarginRight + 1 Then _
minW = .TextRange.BoundWidth + .MarginLeft + .MarginRight + 1
End With
Next
.Columns(icol).Width = minW
Next
End With
End Sub

Cool Properties

Hello John,

I spent ages trying to find a property/method of the "autofit" type for table cells in powerpoint vba. Good to know that there is one. Quite a long way down the chain from the table cell object. I see there is also a boundheight property, for autosizing rows.

To the moderators: is there a way to make submitted vba code more legible, (preferrably with the same appearance as in the VB editor)?

6

BoundHeight and BoundWidth are properties of the textrange and don’t just apply to tables btw

 

Published On: 24th Oct 2015

Read more about -
Forum ,