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

Posted on 2007-04-10
Last Modified: 2012-05-05
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.
Question by:toddvoros
  • 6
  • 5
  • 2

Expert Comment

ID: 18887753

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
LVL 21

Expert Comment

ID: 18889096
Lovely bit of code, Hippohood.

Author Comment

ID: 18889546
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.
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 21

Accepted Solution

GlennaShaw earned 500 total points
ID: 18889898
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.

Author Comment

ID: 18890481
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.

LVL 21

Expert Comment

ID: 18891006
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"

Author Comment

ID: 18891049
Give me a specific code example

Author Comment

ID: 18891124
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.

Author Comment

ID: 18891141
Typo correction: Meant to say you cannot use it on a TEXTFRAME.
LVL 21

Expert Comment

ID: 18891758
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...


Expert Comment

ID: 18896047

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.

Author Comment

ID: 18897488
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
LVL 21

Expert Comment

ID: 18897992
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.

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question