5

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?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
OneNerd
  • 6,442
  • 17
  • 60
  • 78
  • I think I addressed all of your concerns in my edited answer below. – Matthew Jones Jun 08 '09 at 17:35
  • trying to figure out how to get your example to work in my script. I think the (Start:=2, Length:=3) is throwing off VBScript, although it appears to work in VBA (my fault -- should have specified I was using VBScript). Looks like it should work, although what a pain -- I wish they implemented a better way than this. Will post back once I am able to get it working as needed. Thanks - – OneNerd Jun 08 '09 at 19:07
  • I retagged your post to reflect that you are using VBScript. Am interested to know how you solve this problem. – Matthew Jones Jun 08 '09 at 19:39

4 Answers4

6

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
Todd Main
  • 28,951
  • 11
  • 82
  • 146
4

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:

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.

Matthew Jones
  • 25,644
  • 17
  • 102
  • 155
  • 1
    as far as i can tell, that addresses the ENTIRE text range, not individual words inside the text range. – OneNerd Jun 08 '09 at 16:59
3

In addition to the above answer, you should try to name the objects you'll be changing, since selecting them in the middle of a presentation could make PowerPoint act oddly. Create a new TextRange object and set it like this.

dim mytextrange As TextRange
Set mytextrange = ActiveDocument.Pages(1).Shapes(2).TextFrame.TextRange
mytextrange.Words...
Andrew Scagnelli
  • 1,584
  • 4
  • 18
  • 26
0

Here is how you can do it to change the font of a specific text:

Sub changeFont()

Dim oPresentation   As Presentation
Dim oSlide          As Slide
Dim oShape          As Shape
Dim stringSearched  As String    

stringSearched = "something"

'all opened presentations
For Each oPresentation In Presentations
    'all slide in them
    For Each oSlide In oPresentation.Slides
        'all shapes (anything)
        For Each oShape In oSlide.Shapes
            'only those that contain text
            If oShape.HasTextFrame Then
                If InStr(oShape.TextFrame.TextRange.Text, stringSearched) > 0 Then
                    'here you need to define where the text ends and start
                    oShape.TextFrame.TextRange.Characters(InStr(oShape.TextFrame.TextRange.Text, stringSearched), Len(stringSearched)).Font.Underline = msoTrue
                    oShape.TextFrame.TextRange.Characters(InStr(oShape.TextFrame.TextRange.Text, stringSearched), Len(stringSearched)).Font.Italic = msoFalse
                End If
            End If
        Next
    Next
Next
End Sub
Rafiki
  • 604
  • 6
  • 19