Consulting

Results 1 to 3 of 3

Thread: Resize text box to the width of the containing text

  1. #1

    Resize text box to the width of the containing text

    Greetings everyone. I often have a lot of text boxes to fit into some tiny spaces. These boxes are copied from other collegues' slides, there is always some extra space to the both side of the text. Since I have restricted space, I need to somehow narrow down the box horiozontally so that the edges nearly touch the text (but not too narrow so the text wraps into another line).
    Like this:
    Snipaste_2023-02-27_13-22-44.jpg

    I was thinking about resize the shape to the containing text width (in pixel or cm) based on the number of characters (calculated against the font size). But there are two problems:
    1. The text is not in a monospaced font, so we can't simply multiply the character count with some constant value.
    2. The "font size in cm" metric may vary from font to font, so it isn't reliable either.

    What I need is to find out the actual text width within a shape (if the text is on one line)
    Snipaste_2023-02-27_13-22-50.jpg
    I did some search for "POWERPOINT calculate text width" but I think the keywords aren't getting me anywhere near what I need. So I'm asking if it is possible to determine the actual width of a given text.

    Please help!

  2. #2
    I think I found the property: it's boundwidth in the textrange object, so basically we can do something like this:

    Sub ShrinkShapeToTextWidth()
    
    For Each iter In ActiveWindow.Selection.ShapeRange
        iter.Width = iter.TextFrame.TextRange.BoundWidth
    Next
    
    End Sub

  3. #3
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,094
    Location
    Basically but I would add a little more code.

    Sub ShrinkShapeToTextWidth()
    Dim iter As Shape
    For Each iter In ActiveWindow.Selection.ShapeRange
       If iter.HasTextFrame Then
          If iter.TextFrame2.HasText Then
             With iter.TextFrame2
                .WordWrap = False
                .AutoSize = msoAutoSizeNone
             End With
          iter.Width = iter.TextFrame2.TextRange.BoundWidth
          End If
       End If
    Next iter
    End Sub
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •