Share via

PowerPoint Table Text Overflow; How to programmatically detect text overflow within a table cell with VBA?

Anonymous
2023-01-10T17:09:04+00:00

Question is quite similar to the one listed here, and also here, so I'm essentially paging Steve Rinsberg hoping for a last bit of clarification specifically involving VBA and textframe properties for textframes or textframe2s in tables in Powerpoint.

So, I wrote a VBA program that edits a PowerPoint deck which has several tables.

In some cases, the text in certain cells gets wrapped in the middle of a word, and I want to programmatically decrease the font size to the point where it doesn't overflow like that anymore.

I understand that for regular text boxes that are NOT in a table, this can be as simple as

shp.TextFrame2.WordWrap = msoTrue 

shp.TextFrame2.AutoSize = msoAutoSizeTextToFitShape

or something similar.

However, text boxes within tables don't appear to allow this functionality, so what I'm hoping is that TextFrame objects within tables have a property that indicates whether the text is overflowing or not. Checking the value of such a property and decrementing text size based on it should be trivial, but I cannot locate information on what properties are available specifically for TextFrame objects within a table.

I'd also considered somehow allowing the cell size to change to fit the text and detect that instead. I already have code that checks certain cell sizes (row height, specifically), decrements font size, resizes the row, etc. But even trying to programmatically disable WordWrap fails because apparently textframes (and textframe2s) in tables don't have that property (based on the 'specified value out of range' errors I get).

The text is wrapping, so something must be flagging that it needs to wrap, right? What is it?

(Last resort, can VBA automatically paste-special these tables as EMF and un-group them *twice* ? Sounds like a nightmare.)

Thanks in advance!

Microsoft 365 and Office | PowerPoint | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Steve Rindsberg 99,161 Reputation points MVP Volunteer Moderator
2023-01-10T20:10:48+00:00

One thing that might help:

When the text won't fit in the cell, it'll wrap and if wrapping would cause it to fall outside the borders of the cell, PowerPoint will increase the height of the cell to accommodate (actually, the height of the entire row).

You can get a fair idea, though not a totally accurate one, of which cell's text is causing the row height to increase by looking at the bounding box of each cell's text. The bounding box is the smallest rectangle that will just contain the text. I had a go at this with one specific cell on one slide (slide 1, table is the first shape, cell 2,2).

With ActivePresentation.Slides(1).Shapes(1).Table 

    With .Cell(2, 2).Shape 

        Debug.Print .TextFrame.TextRange.Text 

        Debug.Print "Cell coordinates" 

        Debug.Print .Left 

        Debug.Print .Top 

        Debug.Print .Width 

        Debug.Print .Height 

        Debug.Print "Text bounding box" 

        With .TextFrame.TextRange 

            Debug.Print .BoundLeft 

            Debug.Print .BoundTop 

            Debug.Print .BoundWidth 

            Debug.Print .BoundHeight 

            With ActivePresentation.Slides(1).Shapes.AddShape _
            (msoShapeRectangle, .BoundLeft, _
            .BoundTop, .BoundWidth, .BoundHeight) 

                .Line.Visible = msoFalse 

            End With 

        End With 

    End With 

End With

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Steve Rindsberg 99,161 Reputation points MVP Volunteer Moderator
    2023-01-24T14:35:27+00:00

    Excellent! Thanks for coming back with that report.

    We're mostly volunteers here, and this kind of thing doubles our salary. ;-)

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2023-01-24T04:25:27+00:00

    I finally took the time to try to get this working and have been successful, so thanks very much again, Steve.

    In my use case, I'm looking at cells with vertical text that act as row labels. Your code allowed me to report the bounding box parameters for the cells I'm concerned with (there are only a handful, luckily) and determine the maximum allowable BoundWidth (BoundWidth because vertical text) in the worst-case scenario.

    From there it was simple to programmatically compare actual vs max and decrease font size in steps.

    Was this answer helpful?

    0 comments No comments
  3. Steve Rindsberg 99,161 Reputation points MVP Volunteer Moderator
    2023-01-12T20:23:11+00:00

    Glad it helped.

    One gotcha is that the bounding box doesn't match the cell size precisely (probably down to the peculiarities of how fonts are measured ... leading, side-bearings and all that fontstuff). And the right bounding box of left justified type will vary depending on the text and where it wordwraps, of course.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2023-01-11T13:37:14+00:00

    That's exactly the type of arcane knowledge I was looking for - thanks very much, Steve.

    In my scenario, I have the luxury of knowing roughly what the proper bounding box parameters are supposed to be, so I can definitely see a potential solution here. I will let you know if/when I get it working.

    Thanks again!

    Was this answer helpful?

    0 comments No comments