3

I want a textbox where the first line and subsequent lines of text have different formatting, but they must be in the same textbox. This is what I currently have, which applies the same formatting to all text.

Sub geberateSlide() 
  ...
  With currSlide.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, _
  Left:=0, Top:=0, Width:=headerWidth, Height:=headerHeight)
    .TextFrame.TextRange.Text = "Test Box" & vbCrLf & "Description"
    .TextFrame.AutoSize = ppAutoSizeNone
    .Height = headerHeight
    .Line.ForeColor.RGB = RGB(0,0,0)
    .Line.Visible = True
  End With
...
End Sub

The text should be Arial 8. Line 1 should be black and bold, while subsequent text should be blue.

thegreatjedi
  • 2,788
  • 4
  • 28
  • 49

1 Answers1

5

.TextFrame.TextRange.Lines(0, 1) will target the first line.

enter image description here

%300 Zoom

With currSlide.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, _
                                 Left:=0, Top:=0, Width:=headerWidth, Height:=headerHeight)
    .Height = headerHeight
    .TextFrame.AutoSize = ppAutoSizeNone
    With .TextFrame.TextRange
        .Text = "Test Box" & vbCrLf & "Description"
        With .Font
            .Color = vbBlue
            .Size = 8
            .Name = "Arial"
        End With

        With .Lines(1).Font
            .Color = vbBlack
            .Bold = msoTrue
        End With
    End With
End With
  • Thanks for accepting my answer! It was an interesting question. I usually don't use PowerPoint but I have a general idea of the VBA model; so it wasn't too difficult to figure out. –  Nov 14 '16 at 02:54
  • Just a comment that 0 implies the entire paragraph for the legacy TextRange object. So you could get the same result with just .Lines(1) – Shyam Pillai Nov 14 '16 at 03:51
  • 1
    Also, TextRange is legacy object so you need to use TextRange2 to access to the newer text styles. – Shyam Pillai Nov 14 '16 at 03:52
  • @ShyamPillai Thanks I updated `.Lines(1). I had trouble trying to implement `TextRange2`. Maybe it is because for some reason my Office 360 runs in 32bit mode. In any case, `TextRange` is legacy but it is still supported. `TextRange2` only works in Office 2013 and later and there is a lot of people still using Office 2007. –  Nov 14 '16 at 04:14
  • yes, TextRange can be used unless you want to format using the new styles. Also, TextRange2 was introduced in 2007 not 2013. – Shyam Pillai Nov 14 '16 at 15:17