Solved

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

Posted on 2007-04-10
13
2,422 Views
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.
0
Comment
Question by:toddvoros
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
13 Comments
 
LVL 7

Expert Comment

by:hippohood
ID: 18887753
Hi,

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"
        .Sentences
    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
        Loop
    End With
    MsgBox "Font size reduced to fit the box"
End Sub
0
 
LVL 21

Expert Comment

by:GlennaShaw
ID: 18889096
Lovely bit of code, Hippohood.
0
 

Author Comment

by:toddvoros
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 21

Accepted Solution

by:
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: http://skp.mvps.org/ppt00045.htm

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

Author Comment

by:toddvoros
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.





0
 
LVL 21

Expert Comment

by:GlennaShaw
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"
0
 

Author Comment

by:toddvoros
ID: 18891049
Give me a specific code example
0
 

Author Comment

by:toddvoros
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.
0
 

Author Comment

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

Expert Comment

by:GlennaShaw
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.
http://msdn2.microsoft.com/en-us/library/aa221481(office.11).aspx

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...

Glenna
0
 
LVL 7

Expert Comment

by:hippohood
ID: 18896047
toddvoros

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
    ActiveWindow.Selection.ShapeRange.TextFrame.TextRange.Select
===============================================

Glenna:
Greatly appreciate your note. A nice WEB site you have. Have used a few tricks of yours already.
Chears
0
 

Author Comment

by:toddvoros
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)
       
    Else
   
        msgbox("Object is not a textbox or does not have a TextFrame")    
       stop
    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
0
 
LVL 21

Expert Comment

by:GlennaShaw
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.
:-)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

Suggested Solutions

This is an explanation of a simple data model to help parse a JSON feed
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

752 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