Search code examples
powerpointshapes

AddTextBox in PowerPoint


Assuming slideShape is a reference to a Shapes object, to create a text box in a PPT slide, I can use the following code:

slideShape.AddTextBox(Orientation, left, top, width, height)
slideShape.AddTextBox.Text = 'ABC-123 Feb 2015 Mike Smith'

So far so good. But if I want to break the text in 3 lines:

ABC-123 
Feb 2015 
Mike Smith

and I need to color, re-size, and apply a different font style to each line, I could write three separate slideShape.AddTextBox calls, but doing that will create 3 separate text boxes.

Is it possible to write 3 separate lines in one text box? I don't think AddTextBox allow me to do that. I know it can be done by using some other methods, but I'm not sure how.

Any advice?


Solution

  • Sub Thing()
    
        ' Some setup to add a text box
        Dim oSl As Slide
        Dim oSh As Shape
    
        Set oSl = ActivePresentation.Slides(1)
        Set oSh = oSl.Shapes.AddTextbox(msoTextOrientationHorizontal, 0, 0, 500, 500)
    
        ' But add the tex like so ... with a CR/LF pair at the end of every line:
        oSh.TextFrame.TextRange.Text = "ABC-123" & vbCrLf & "Feb 2015" & vbCrLf & "Mike Smith"
    
        ' The shape's TextRange has a .Paragraphs collection that you can address
        ' a paragraph at a time.
        ' Note: there's also a .Lines collection
        With oSh.TextFrame.TextRange
            .Paragraphs(1).Font.Color.RGB = RGB(255, 0, 0)
            .Paragraphs(2).Font.Color.RGB = RGB(0, 255, 0)
            .Paragraphs(3).Font.Color.RGB = RGB(0, 0, 255)
        End With
    
    End Sub