2015-03-31 40 views
0

標題幾乎說明了一切,但爲了使它更清楚一點,說我想創建一個文本框通過VBA說:「這個文本應該是字體大小爲24,這個文本應該是字體大小20。「在同一個文本框中使不同的單詞有不同的字體大小使用VBA

現在,我正在使用自己的函數來創建下面的文本框。歡呼,並感謝您的幫助!

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 
+0

我相信你需要使用RichTextBox或兩個文本框來實現這一點。 – 2015-03-31 19:41:38

回答

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 
+0

很好的答案,謝謝! – spaderdabomb 2015-03-31 22:46:45

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 
相關問題