使用VBA使同一文本框中的不同单词具有不同的字体大小

时间:2015-03-31 19:15:47

标签: vba powerpoint

标题几乎说明了一切,但为了使它更清晰,说我想通过VBA创建一个文本框,说“这个文本应该是24的字体,这个文本应该是字体大小20 。“

现在,我正在使用我自己的函数来创建文本框,如下所示。干杯,谢谢你的帮助!

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

2 个答案:

答案 0 :(得分:3)

不需要RichTextBox。答案在于TextBox的TextFrame中的TextRange对象的属性(多么令人满意!)。基本上,您可以解析/遍历此范围对象中的文本,如果您根据段落(或句子,单词,字符等)进行选择,则可以应用不同的文本效果。

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

答案 1 :(得分:0)

获取文本范围参考,然后指定所需的字体大小。

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