Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply Font Formatting to PowerPoint Text Programmatically

I am trying to use VBA to insert some text into a PowerPoint TextRange, I use something like this:

ActiveWindow.Selection.SlideRange.Shapes("rec1").TextFrame.TextRange.Text = "Hi"

However, I can't figure out how to apply bold, italic and underline programmatically (I don't see a .RichText property or something similar).

What I have is some simple HTML text with bold, italic and underlined text I would like to convert over.

How to do this?

like image 208
OneNerd Avatar asked Jun 08 '09 15:06

OneNerd


2 Answers

This is easily accomplished by using the TextRange's Characters, Words, Sentences, Runs and Paragraphs objects and then it's Font object to set Bold, Underline and Italic (amongst other properties). For example:

Sub setTextDetails()
    Dim tr As TextRange
    Set tr = ActiveWindow.Selection.SlideRange.Shapes(1).TextFrame.TextRange
        With tr
            .Text = "Hi There Buddy!"
            .Words(1).Font.Bold = msoTrue
            .Runs(1).Font.Italic = msoTrue
            .Paragraphs(1).Font.Underline = msoTrue
        End With
End Sub
like image 147
Todd Main Avatar answered Sep 30 '22 19:09

Todd Main


Try looking at MSDN's documentation on the TextRange object. It contains samples of how to access the Font properties of the TextRange object.

EDIT: You can access things like Bold and Italics programmatically in this manner:

TextRange.Font.Bold = msoTrue

EDIT EDIT: There are several methods by which you can select only certain text in a text range. See the following:

  • Characters Method
  • Lines Method
  • Paragraphs Method
  • Words Method

According to the sames from this link, you can select a portion of the text using one of these methods and set the font programmatically. For example:

Application.ActiveDocument.Pages(1).Shapes(2) _
.TextFrame.TextRange.Words(Start:=2, Length:=3) _
.Font.Bold = True

That example was taken from the Words Method link.

like image 31
Matthew Jones Avatar answered Sep 30 '22 19:09

Matthew Jones