How to get ScaleWidth and ScaleHeight for a Textbox in PowerPoint?

Need an example of VBA code to get ScaleHeight and ScaleWidth for an existing TextBox on a Slide
in PowerPoint? Retrieving Height and Width and rescaling only works for Pictures and OLE objects,
not TextBoxes.  There must be some way to do this, but is is not obvious.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


Text boxes have these properties see an example below. However, the scaling is applied to Text Frame only (you have to modify text to fit in the smaller box size). Also, you can keep "realtive to original" only for OLEs and pics. Citing: "Shapes other than pictures and OLE objects are always scaled relative to their current width".

The code below shows how to use this method and a way how to make the text to fit it in. Other ways can be imagined.

Sub InsertNeatBox()
Dim aBox As Shape
    Set aBox = ActiveWindow.Selection.SlideRange.Shapes.AddTextbox(msoTextOrientationHorizontal, 100, 300, 200, 40)
    With aBox.TextFrame.TextRange
        .Text = "This is a box" + Chr$(CharCode:=13) + "with three" + Chr$(CharCode:=13) + "lines"
    End With
    MsgBox "Textbox created"
    With aBox
        .ScaleWidth 0.2, msoFalse
    End With
    MsgBox "Scaled down - text doesn't fit the box"
    With aBox.TextFrame.TextRange
        Do While ((.BoundHeight > aBox.Height) Or (.BoundWidth > aBox.Width))
                .Font.Size = .Font.Size - 1
    End With
    MsgBox "Font size reduced to fit the box"
End Sub
Lovely bit of code, Hippohood.
toddvorosAuthor Commented:
I need to RETRIEVE the ScaleWidth / ScaleHeight from an EXISTING TextFrame from a presenation
that I did not create.  The code you supplied (which I am already familiar with) only shows how
to SET the scaling for the TextFrame, not how to retrieve it.
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Retrieving scaleheight and scalewidth should also work for shapes.  The textfame should be in a bounding box within a shape.  See this info about bounding boxes:

And hippohood did provide you with a lovely bit of code even if it wasn't exactly what you were looking for.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
toddvorosAuthor Commented:
BoundHeight and BoundWidth does not give me what I need.  Here is what I need.

In PowerPoint, a Textbox has been created originally that looks like this
where *** represents the border lines:

* A text string *

The user then extends the lines surrounding the Text box like this:

* A text string                                           *

Internally, this is typically done by

ActiveWindow.Selection.ShapeRange.ScaleWidth 1.55, msoFalse, msoScaleFromTopLeft
(where 1.55 is the scaling factor for the increase in the width of the text box)

What is delivered to me in a presentation is the streched textbox.

I need the code that RECOVERS the value of 1.55 (the original ScaleWidth factor).

Thank you for your help on this.  I had hoped that BoundWidth would do this,
but BoundWidth does not change at all when the textbox is streched, so I
cannot recover the orginal ScaleWidth factor via object.TextFrame.TextRange.BoundWidth.

But you should be able to recover it from the shape.

"All positional/dimensional properties behave exactly identical to the properties of the parent shape"
toddvorosAuthor Commented:
Give me a specific code example
toddvorosAuthor Commented:
This code would provide the answer EXCEPT you cannot use it on a TextRange, darn it!!
It only works on Pictures or OLE objects. So I need code similar to this that WILL work for a TextRange.
    Dim CurrentWidth As Single
    Dim OriginalWidth As Single
    Dim ShapeType As String
    CurrentWidth = ppShape.Width
    ppShape.ScaleWidth 1, True
    OriginalWidth = ppShape.Width
    PowerPointScaleWidth = CurrentWidth / OriginalWidth
    Rem Rescale the object back to it's scaled Width:
    ppShape.ScaleWidth PowerPointScaleWidth, True

Whatever solution is provided, I need a specific code example to test. Thank you.
toddvorosAuthor Commented:
Typo correction: Meant to say you cannot use it on a TEXTFRAME.
Perhaps you can use the parent property to get the info you need and return the parent shape to the original scalewidth and set the textframe width to the match the parent shape width or reset the textframe margins.

Sorry, can't give you the code example, just not that good at it and don't have the time right now.  Why I admire folks like Hippohood...


Sorry we didn't understand each other. I cite again the article from the VBA Help
ScaleHeight Method
Scales the height of the shape by a specified factor. For pictures and OLE objects, you can indicate whether you want to scale the shape relative to the original size or relative to the current size. Shapes other than pictures and OLE objects are always scaled relative to their current height.
That means that information about original Textbox size is never stored, It makes sense, if you think about it. You just don't need it for Shapes, the are needed for hard-to-scale-back objects (Images and OLEs). Note, that you still would have uncertainty of the value of MsoScaleFrom.

You may try to get the original size back by the following ways:
1. Set the shape width eqaul to text width
aBox.Width= aBox.BoundWidth
2. If this textbox was on Master slide (doupt it) get it back to Master value
aBox.Width= .SlideMaster.Shapes("Rectangle 1").Width

Greatly appreciate your note. A nice WEB site you have. Have used a few tricks of yours already.
toddvorosAuthor Commented:
Here is tested, verified solution which I wrote myself to get the ScaleWidth of a streched Textbox,
after much experimenting.  The fact the empty textboxes have a default width that is independent
of the font specified must be taken into account when computing the ScaleWidth.
This is obscure and not documented anywhere that I could find. BoundWidth is needed to
derive ScaleWidth of the TextBox, so I will award the points to Glenna for pointing me to
the BoundWidth property. However, all of the code is my own.

Function PowerPointScaleWidth(Calledby As String, ppShape As PowerPoint.Shape) As Single

    On Error GoTo abort
    Rem *************************************************************
    Rem * Compute the ScaleWidth for the specified PowerPoint shape *
    Rem *************************************************************
    Rem Version A00 - 10-Apr-07 - Todd Voros - Initial version
    Rem Version A01 - 10-Apr-07 - Todd Voros - Fix incorrect reference name for ppShape
    Rem Version B00 - 10-Apr-07 - Todd Voros - Call PowerPointShapeType
    Rem Version D00 - 11-Apr-07 - Todd Voros - Support TextBoxes (major enhancement)
    Rem Version D01 - 11-Apr-07 - Todd Voros - Change PowerPointScaleWidth precission from Single to Double
    Rem Version E00 - 11-Apr-07 - Todd Voros - Treat PlaceHolder Shape as a TextBox if it has a TextFrame
    Rem Version F00 - 11-Apr-07 - Todd Voros - Round PowerPointScaleWidth to two decimal places
    Dim CurrentWidth As Single
    Dim BoundWidth As Single
    Dim dblCurrentWidth As Double
    Dim OriginalWidth As Double
    Dim ShapeType As String
    Const S_Edge = 15.25                    ' Total Space in Width units for default Empty Textbox with Autofit applied
    Dim Caller As String
    Caller = "PowerPointScaleWidth"
    CurrentWidth = ppShape.Width            ' Must be retrieved into a Single.
    dblCurrentWidth = CDbl(CurrentWidth)    ' Convert Single to Double precission.
    ShapeType = PowerPointShapeType(Caller, ppShape)
    If (LCase$(ShapeType) = "textbox" Or (ppShape.HasTextFrame = True)) Then
        Rem ***************************************************************
        Rem * CurrentWidth is the actual width of the displayed border    *
        Rem * around the TextBox *after* any ScaleWidth has been applied. *
        Rem ***************************************************************
        BoundWidth = ppShape.TextFrame.TextRange.BoundWidth
        OriginalWidth = BoundWidth + S_Edge
        PowerPointScaleWidth = dblCurrentWidth / OriginalWidth
        PowerPointScaleWidth = Round(PowerPointScaleWidth, 2)
        msgbox("Object is not a textbox or does not have a TextFrame")    
    End If
    Exit Function

abort: Call dead("PowerPointScaleWidth")

End Function
Function PowerPointShapeType(Calledby As String, ppShape As PowerPoint.Shape) As String

    On Error GoTo abort
    Rem Return the type of Powerpoint Shape as a string
    Rem Version A00 - 10-Apr-07 - Todd Voros - Initial version.
    Dim ShapeCode As Long
    Dim Caller As String
    Caller = "PowerPointShapeType"
    PowerPointShapeType = ""    ' Indicate code not known

    ShapeCode = ppShape.Type    ' Get the Shape code
    If ShapeCode = msoAutoShape Then PowerPointShapeType = "AutoShape"
    If ShapeCode = msoCanvas Then PowerPointShapeType = "Canvas"
    If ShapeCode = msoComment Then PowerPointShapeType = "Comment"
    If ShapeCode = msoFormControl Then PowerPointShapeType = "FormControl"
    If ShapeCode = msoCallout Then PowerPointShapeType = "Callout"
    If ShapeCode = msoFormControl Then PowerPointShapeType = "FormControl"
    If ShapeCode = msoChart Then PowerPointShapeType = "Chart"
    If ShapeCode = msoEmbeddedOLEObject Then PowerPointShapeType = "EmbeddedOLEObject"
    If ShapeCode = msoFreeform Then PowerPointShapeType = "msoFreeform"
    If ShapeCode = msoGroup Then PowerPointShapeType = "Group"
    If ShapeCode = msoLine Then PowerPointShapeType = "Line"
    If ShapeCode = msoLinkedOLEObject Then PowerPointShapeType = "LinkedOLEObject"
    If ShapeCode = msoLinkedPicture Then PowerPointShapeType = "LinkedPicture"
    If ShapeCode = msoMedia Then PowerPointShapeType = "Media"
    If ShapeCode = msoOLEControlObject Then PowerPointShapeType = "OLEControlObject"
    If ShapeCode = msoPicture Then PowerPointShapeType = "Picture"
    If ShapeCode = msoPlaceholder Then PowerPointShapeType = "Placeholder"
    If ShapeCode = msoScriptAnchor Then PowerPointShapeType = "ScriptAnchor"
    If ShapeCode = msoShapeTypeMixed Then PowerPointShapeType = "ShapeTypeMixed"
    If ShapeCode = msoTable Then PowerPointShapeType = "Table"
    If ShapeCode = msoTextBox Then PowerPointShapeType = "TextBox"
    If ShapeCode = msoTextEffect Then PowerPointShapeType = "TextEffect"
    If PowerPointShapeType = "" Then
        MsgBox ("PowerPointShapeCode: Unknown Shape code " & ShapeCode)
        Call dead(Caller)
    End If
    Exit Function
abort: Call dead("PowerPointShapeType")

End Function
Well thank you, sir.  And thanks for writing and sharing the code that will be helpful for someone else who might have a related issue.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft PowerPoint

From novice to tech pro — start learning today.