Results 1 to 9 of 9
  1. #1
    Star Lounger
    Join Date
    Sep 2006
    Location
    New York, New York, USA
    Posts
    76
    Thanks
    1
    Thanked 0 Times in 0 Posts

    VBA: Flowing Shapes From One Slide To The Next

    Apologies in advance for the newbie question. I am writing a VB app to automate some simple PowerPoint rendering. I want to create some images on a slide (either from the Clipboard or from a file) and have some text appearing under the images.

    I figured out how to create shapes to contain these items, but the one thing I haven't figured out is how to get a shape containing text to flow onto a new slide if the text does not fit in the shape on the same slide. Currently, the text flows off the slide, extending the height of the slide beyond the bottom of the slide.

    I DON'T want to shrink the text to fit since there could be a considerable amount of text and don't want it readable only by ants.

    Any suggestions?

    Thanks!

  2. #2
    3 Star Lounger
    Join Date
    Feb 2007
    Location
    Durham, Durham, United Kingdom
    Posts
    286
    Thanks
    0
    Thanked 10 Times in 10 Posts
    You would need to restrict the shape to x lines (with code) and place any more on a new slide.

    My advice is post the version you have and any code so far.

  3. #3
    Star Lounger
    Join Date
    Sep 2006
    Location
    New York, New York, USA
    Posts
    76
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks, John. I found some code on the Net that resizes the content to fit the shape, but this is not ideal. Can you suggest how I would implement your suggestion? A few lines of code to get me going? I am using Office 2010, but this will need to work for versions back to 2003 as well.

    Thanks again!

  4. #4
    3 Star Lounger
    Join Date
    Feb 2007
    Location
    Durham, Durham, United Kingdom
    Posts
    286
    Thanks
    0
    Thanked 10 Times in 10 Posts
    Like I said post the code you have to add the pic and text and I'll see if it can be adapted.

  5. #5
    Star Lounger
    Join Date
    Sep 2006
    Location
    New York, New York, USA
    Posts
    76
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi John,<br>
    <br>
    Below is the code I have for resizing the text to fit its containing shape. I am not including the image handling since I know how to do that. The code currently resizes the font size up or down to fit the shape -- I had gotten this off the Net. It works well for what it does, but is not how I want to handle text that would overflow a shape and the slide. My goal is to somehow detect the number of lines that a shape will fit before overflowing, then create one or more slides with shapes to accommodate the overflowing text. The code below has no relationship to my goal, but you requested it so here it is. Thanks for any insights!<br>
    <br>
    Code:
    <br>Private Sub ResizeText(ByRef myShape As Object)<br>
    <br>
        Dim oShp As Object<br>
        Dim sHeight As Single<br>
    <br>
        Set oShp = myShape <br>
        If oShp.HasTextFrame Then<br>&nbsp;&nbsp; sHeight = oShp.Height<br>&nbsp;&nbsp; oShp.TextFrame.AutoSize = ppAutoSizeShapeToFitText<br>&nbsp;&nbsp; If oShp.Height &gt; sngHeight Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                Do<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    oShp.TextFrame.TextRange.Font.Size = oShp.TextFrame.TextRange.Font.Size - 1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    If oShp.TextFrame.TextRange.Font.Size = 1 Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        Exit Do<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                Loop While oShp.Height &gt; sHeight <br>&nbsp;&nbsp;
            Else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                Do<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    oShp.TextFrame.TextRange.Font.Size = oShp.TextFrame.TextRange.Font.Size + 1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    If oShp.TextFrame.TextRange.Font.Size &gt; 72 Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        Exit Do<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    End If<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                Loop While oShp.Height &lt; sHeight <br>&nbsp;&nbsp;
            End If<br>&nbsp;&nbsp;
            oShp.TextFrame.AutoSize = ppAutoSizeNone<br>
        End If<br>
    <br>
    End Sub<br>

  6. #6
    3 Star Lounger
    Join Date
    Feb 2007
    Location
    Durham, Durham, United Kingdom
    Posts
    286
    Thanks
    0
    Thanked 10 Times in 10 Posts
    Actually I was looking for the code you used to add the pic and text also your code is unreadable with the HTML tags!

    Anyway maybe this will help:


    Sub textflow()
    Dim osld As Slide
    Dim otxtB As Shape
    Dim strOverflow As String
    Set osld = ActivePresentation.Slides(1)
    Set otxtB = osld.Shapes.AddTextbox(msoTextOrientationHorizonta l, 10, 300, 400, 200)

    With otxtB.TextFrame.TextRange
    .Font.Size = 44
    .Text = "The quick brown fox jumped over the lazy dog" & _
    " who was wondering why there was a fox in the living room"
    .ParagraphFormat.WordWrap = True

    If .Lines.Count > 3 Then
    strOverflow = .Lines(4, 3)
    .Text = .Lines(1, 3)
    End If
    End With
    If strOverflow <> "" Then
    Set osld = ActivePresentation.Slides.Add(2, ppLayoutBlank)
    Set otxtB = osld.Shapes.AddTextbox(msoTextOrientationHorizonta l, 10, 300, 400, 200)
    With otxtB.TextFrame.TextRange
    .Font.Size = 44
    .Text = strOverflow
    End With
    End If
    End Sub
    Last edited by JohnWilson; 2011-07-20 at 04:55.

  7. #7
    Star Lounger
    Join Date
    Sep 2006
    Location
    New York, New York, USA
    Posts
    76
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi John,

    Thanks very much for the sample code. I see how this works, but only if the font size and lines limit are known in advance -- hard-coded, essentially. However, I need to assume that font sizes (and font names) can change, as well as the size of the containing shape.

    So what I need is a routine that will determine if text overflow will occur, create the additional slides/shapes as necessary, and parse the text into each of these shapes, all based on the metrics of the shape's font. Does PowerPoint have any built-in properties/methods to do the heavy lifting of calculating how much text will fit into the shape?

  8. #8
    3 Star Lounger
    Join Date
    Feb 2007
    Location
    Durham, Durham, United Kingdom
    Posts
    286
    Thanks
    0
    Thanked 10 Times in 10 Posts
    If you are adding the text with code (Which you STILL haven't posted!!) how could you not know the font size?

    In any case you could use .Textframe.TextRange.BoundHeight to determine if you need to overflow.

  9. #9
    Star Lounger
    Join Date
    Sep 2006
    Location
    New York, New York, USA
    Posts
    76
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by JohnWilson View Post
    If you are adding the text with code (Which you STILL haven't posted!!) how could you not know the font size?

    In any case you could use .Textframe.TextRange.BoundHeight to determine if you need to overflow.
    I did paste the code, but unfortunately it displayed many tags for some reason.

    Here is a new sample that covers the essence of what I am doing. I left out quite a bit of supporting code for the sake of brevity and have added comments where appropriate.

    This code dynamically builds a presentation by adding slides containing a single shape (the "real" application has a few shapes, but that is irrelevant for this thread). A textbox is added to each shape. This sample assumes that there is some html content -- simple formatting like bold or italics -- already on the Clipboard (in the "real" application, content changes for each slide; here, it just pastes the same content for each slide).

    Regarding your question "how could you not know the font size?", the content that is pasted for each slide is different. Since it is HTML content it can essentially have different fonts/sizes for each line of text being pasted. Also, we can have different templates that would format the presentation differently, so I need to write this generically so that any content can be pasted into any type of presentation and the macro code will handle the overflow elegantly -- i.e. create additional slides/shapes as necessary to contain the entire text for a given record, no matter what font/size the content is formatted with.

    Lastly, if I understand your point about BoundHeight, are you saying that in order for me to achieve the above goal I should use the following formula: x = .Shapes(1).Textframe.TextRange.BoundHeight / .Shapes(1).Height -- if x <= 1 then do nothing (meaning, the content fits into one shape); otherwise, create x - 1 additional slides and shapes to contain the overflow text. Correct? I see how that tells me how many shapes I need. I suppose to parse the text into each additional shape I can use the Lines property (after first determining how many lines fit into the first shape), correct?

    Here's my sample code, hopefully it appears correctly:

    Code:
    Sub CreatePresentation()
    
        Dim oApp As Application
        Dim oPres As Presentation
        Dim oSlide As Slide
        Dim oShape As Shape
        Dim i As Integer
    
        Set oApp = New Application
        Set oPres = oApp.Presentations.Add
        oApp.Visible = True
        oApp.Activate
        
        'hard-code the slide orientation -- "real" app may use templates with preset slides/shapes/fonts
        oPres.PageSetup.SlideSize = ppSlideSizeOnScreen
        oPres.PageSetup.SlideOrientation = msoOrientationVertical
        
        For i = 1 To 20  'for illustration purposes only
           Set oSlide = oPres.Slides.Add(i, Layout:=ppLayoutBlank)
           oSlide.Select
           Set oShape = oSlide.Shapes.AddTextbox(msoTextOrientationHorizontal, 75, 290, 400, 400)  'hard-coded, for illustration purposes only
           oShape.TextFrame.AutoSize = ppAutoSizeNone
           oShape.Select
           'assume that different html content has been copied to Clipboard for each iteration of the loop
           oShape.TextFrame.TextRange.PasteSpecial ppPasteHTML
           oApp.ActiveWindow.Selection.Unselect
        Next
      
    End Sub

Posting Permissions

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