spaderdabomb
spaderdabomb

Reputation: 942

Make different words in same text box have different font size using VBA

the title pretty much says it all, but to make it a little more clear, say I want to create a text box via VBA that says "This text should be of font size 24, this text should be of font size 20."

Right now, I'm using my own function to create the text box, which is below. Cheers, and thanks for the help!

Sub textBox(textBoxText As String)
    Dim myTextBox As Shape
    With ActiveWindow.Selection.SlideRange
        Set myTextBox = .Shapes.AddTextbox _
            (Orientation:=msoTextOrientationHorizontal, Left:=153, Top:=50, _
            Width:=400, Height:=100)
        myTextBox.TextFrame.TextRange.Text = textBoxText
        myTextBox.TextFrame.TextRange.Paragraphs.ParagraphFormat.Alignment = ppAlignCenter
        myTextBox.TextFrame.TextRange.Font.Bold = msoTrue
        myTextBox.TextFrame.TextRange.Font.Name = "Arial (Headings)"

    End With
End Sub

Upvotes: 0

Views: 6262

Answers (2)

Shyam Pillai
Shyam Pillai

Reputation: 586

Get the text range reference and then assign the desired font size.

With myTextBox.TextFrame2.TextRange
    With .InsertAfter("This text should be of font size 24,")
        .Font.Size = 24
    End With
    With .InsertAfter("this text should be of font size 20")
        .Font.Size = 20
    End With
End With

Upvotes: 0

PeterT
PeterT

Reputation: 8557

A RichTextBox is not needed. The answer lies in the properties of the TextRange object within the TextFrame of the TextBox (what a mouthful!). Basically, you can parse/traverse the text within this range object and, if you make selections based on paragraphs (or sentences, words, characters, etc) you can apply different text effects.

Sub CreateTextbox()
    Dim MyTextBox As Shape
    Dim textBoxText As String
    Dim textToChange As TextRange
    textBoxText = "this is some wild text"
    With ActiveWindow.Selection.SlideRange
        Set MyTextBox = .Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, _
                        Left:=153, Top:=50, Width:=400, Height:=100)
        MyTextBox.TextFrame.TextRange.Text = textBoxText
        MyTextBox.TextFrame.TextRange.Paragraphs.ParagraphFormat.Alignment = ppAlignCenter
        MyTextBox.TextFrame.TextRange.Font.Bold = msoTrue
        MyTextBox.TextFrame.TextRange.Font.Name = "Arial (Headings)"

        # here's where the magic happens
        Set textToChange = MyTextBox.TextFrame.TextRange
        textToChange.Words(3).Select
        textToChange.Words(3).Font.Size = 42

    End With
End Sub

Upvotes: 3

Related Questions