How to define title text and subtitle text in VBA for PowerPoint 2010

HotDogCannon picture HotDogCannon · Jan 8, 2014 · Viewed 16.6k times · Source

I have the following code (VBA for PowerPoint 2010) to create a slide at the end of a presentation and insert title text:

longSlideCount = ActivePresentation.Slides.Count

With ActivePresentation.Slides
    Set slideObject = .Add(longSlideCount + 1, ppLayoutTitleOnly)
End With

slideObject.Shapes.Title.TextFrame.TextRange.Text = "This is the Main Title Text"

I would like to know how to:

  • Insert 'Subtitle' text (smaller font, on a new line immediately below the main title text)
  • Change the font and size of the main title text and the subtitle text

Thanks in advance!!!

Answer

Olle Sjögren picture Olle Sjögren · Jan 8, 2014

You can do it in several ways depending on how you want the slide to look. The easiest may be to use the ppLayoutTitle instead of ppLayoutTitleOnly. It has two textframes instead of one, so you could update the text by using something like the following:

slideObject.Shapes(2).TextFrame.TextRange.Text = "This is the subtitle."

If you need a more custom layout, you could add a new textbox by using adding the following at the bottom of your code example:

Set oShp = slideObject.Shapes.AddTextbox(msoTextOrientationHorizontal, 100, 100, 200, 300)
oShp.TextFrame.TextRange.Text = "Row #2: subtitle."

The numeric parameters to the AddTextbox function set the position and size of the new textbox.

You can update the font the same way for both of the above examples (just change the reference to the textbox):

oShp.TextFrame.TextRange.Font.Bold = msoTrue