1
votes

I'm attempting to create a textbox at the top of the slide in powerpoint using VBA (so much about that sentence makes me sad). A lot of the examples I see online don't seem like they use "Option explicit" macro practices because some of the variables I see in examples aren't declared or expressly typed.

Anyways, at this point I just need to generate a textbox, ideally get some kind of name for this text box so I could do things with it, and then modify the parameters of this textbox, like font size. Eventually I'm going to make it so users can input the string values and create their own sheets, using this as a template.

Currently I'm working with this. myDocument is the slide, and title is a string that I've already assigned. This would be absolutely what I need but I can't change anything else about it, like font size.

myDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, Left:=0, Top:=10, Width:=200, Height:=50).TextFrame.TextRange.Text = title

If I wanted to add .Font.Size = 18 to the attributes, I can't seem to find a way that has worked so far.

I've seen a few examples where they use a with - end with statement and set individual properties, but when I tried to create a shape and then modify individual attributes, I'd get errors(probably because shapes don't have a font attribute). Here's a page I've been trying to use as a guideline but it hasn't helped much

http://www.ozgrid.com/forum/showthread.php?t=18611

I'm pretty lost on how to use text box objects, how to keep track of them or assign them a name, pretty much everything about them. This is entirely new territory for me. Any help would be immensely appreciated

1

1 Answers

1
votes

It will help to understand the hierarchy of objects in PowerPoint. Once you grok that, most of the ways you get to things in the object model make a lot more sense.

The application itself contains a Presentations collection of which each open presentation is a member.

ActivePresentation is whichever presentation is currently active ... ie, has focus, receives mouseclicks/keystrokes.

A Presentation contains Slides Slides contain Shapes Most, though not all, shapes can have a TextFrame TextFrame contains a TextRange And the TextRange contains the text (and you apply formatting to the text range as well)

So:

Option Explicit  
' but of course!

Sub MakeATitle()

    Dim oSh As Shape
    Dim myDocument As Presentation
    Dim oSl As Slide
    Dim sTitle As String
    ' I wouldn't use Title as a variable; it may be a reserved word

    sTitle = "Here is your title"

    Set myDocument = ActivePresentation

    ' I'm putting the title on slide 1
    ' Change this as needed, of course
    Set oSl = myDocument.Slides(1)

    Set oSh = oSl.Shapes.AddTextbox(msoTextOrientationHorizontal, _
        Left:=0, Top:=10, Width:=200, Height:=50)

    With oSh.TextFrame.TextRange
        .Text = sTitle
        With .Font
            .Size = 24  ' points
            .Name = "Arial"
        End With    ' Font
    End With    ' TextRange
End Sub