Microsoft Office 2000/Visual Basic Programmer's Guide   

Working with Shapes on Slides

Just as a PowerPoint presentation consists of a collection of slides, a PowerPoint slide typically consists of one or more Shape objects. Whether a slide contains a picture, a title, text, an OLE object, an AutoShape, or other content, everything on the slide is a Shape object.

You can refer to a Shape object on a slide in two ways:

To work with multiple shapes on a slide, you use the Range method of the Shapes collection. The Range method returns a ShapeRange object containing the shapes specified in the method's argument. If no Index argument is supplied, the Range method returns a ShapeRange object containing all the shapes on a slide. To specify multiple shapes, you can use the VBA Array function. For more information about using the Array function to specify multiple items, see the discussion of the Array function in the previous section, "Working with Slide Objects."

Adding Shapes to Slides

Typically, you use the Add method of a collection object to add an item to the collection. For example, to add a slide to a PowerPoint presentation, you use the Presentation object's Slides collection's Add method. However, adding shapes to a slide is a little different. The PowerPoint object model provides a different method for each shape you can add to a slide. For example, the following sample inserts a new slide at the end of the current presentation and uses two methods of the Shapes collection to add shapes to the slide. The AddTextEffect method is used to add a WordArt shape and the AddTextbox method is used to add a text box shape:

Sub AddTestSlideAndShapes()
   ' Illustrate how to add shapes to a slide and then
   ' center the shapes in relation to the slide and
   ' each other.
   Dim sldNewSlide         As PowerPoint.Slide
   Dim shpCurrShape        As PowerPoint.Shape
   Dim lngSlideHeight      As Long
   Dim lngSlideWidth       As Long
   
   With ActivePresentation
      ' Determine height and width of slide.
      With .PageSetup
         lngSlideHeight = .SlideHeight
         lngSlideWidth = .SlideWidth
      End With
      ' Add new slide to end of presentation.
      Set sldNewSlide = .Slides.Add(.Slides.Count + 1, ppLayoutBlank)
      With sldNewSlide
         ' Specify a background color for the slide.
         .ColorScheme = ActivePresentation.ColorSchemes(3)
         ' Add a WordArt shape by using the AddTextEffect method.
         Set shpCurrShape = .Shapes.AddTextEffect(msoTextEffect16, _
            "Familiar Quotations", "Tahoma", 42, msoFalse, msoFalse, 100, 100)
         ' Locate the WordArt shape at the middle of the slide, near the top.
         With shpCurrShape
            .Left = (lngSlideWidth - .Width) / 2
            .Top = (lngSlideHeight - .Height) / 8
         End With
         ' Add a Textbox shape to the slide and add text to the shape.
         Set shpCurrShape = .Shapes _
            .AddTextbox(msoTextOrientationHorizontal, 100, 100, 500, 500)
         With shpCurrShape
            With .TextFrame.TextRange
               .Text = "'If not now, when? If not us, who?'" _
                  & vbCrLf & "'There is no time like the present.'" _
                  & vbCrLf & "'Ask not what your country can do for you, " _
                  & "ask what you can do for your country.'"
               With .ParagraphFormat
                  .Alignment = ppAlignLeft
                  .Bullet = msoTrue
               End With
               With .Font
                  .Bold = msoTrue
                  .Name = "Tahoma"
                  .Size = 24
               End With
            End With
            ' Shrink the Textbox to match the text it now contains.
            .Width = .TextFrame.TextRange.BoundWidth
            .Height = .TextFrame.TextRange.BoundHeight
            .Left = (lngSlideWidth - .Width) / 2
            .Top = (lngSlideHeight - .Height) / 2
         End With
      End With
   End With
End Sub

The AddTestSlideAndShapes procedure is available in the modShapeCode module in Shapes.ppt in the ODETools\V9\Samples\OPG\Samples\CH05 subfolder on the Office 2000 Developer CD-ROM.

To see an example of how to determine the types of Shape objects a slide contains as well as some of the properties of a particular shape, you can use the GetShapeInfo procedure. The GetShapeInfo procedure is available in the modShapeCode module in Shapes.ppt in the ODETools\V9\Samples\OPG\Samples\CH05 subfolder on the Office 2000 Developer CD-ROM.

Positioning Shapes on Slides

When you add a shape to a slide, the method you use typically requires you to specify values to establish the dimensions of the shape. In some cases, as with the AddTextEffect method (illustrated in the AddTestSlideAndShapes procedure shown earlier in "Adding Shapes to Slides"), you specify values for the Left and Top properties of the shape (the height and width of the shape is determined by the text it contains). In other cases (as with the AddTextbox method, also illustrated in the AddTestSlideAndShapes procedure), you must specify values for the Shape object's Left, Top, Width, and Height properties.

The height and width of shapes are specified in pixels. The default slide size is 720 pixels wide and 540 pixels high. The center of a slide is 360 pixels from the left edge of the slide and 270 pixels from the top of the slide. You can center any shape horizontally by using the formula (SlideWidth - ShapeWidth) / 2. You can center any shape vertically by using the formula (SlideHeight - ShapeHeight) / 2. You can programmatically specify or determine the height and width setting for the slides in a presentation by using the Presentation object's PageSetup property to return a PageSetup object, and then use the PageSetup object's SlideHeight and SlideWidth properties. This technique is also illustrated in the AddTestSlideAndShapes procedure shown earlier.

To position one or more shapes on a slide either in relation to the slide or to other shapes on the slide, you can use the Align or Distribute methods of a ShapeRange object.

Working with Text in a Shape

Much of what you do with shapes on slides involves adding or modifying text. In addition to the Textbox shape, many other Shape objects can contain text. For example, you can add text to many of the AutoShape Shape objects.

All shapes that support text have a TextFrame property you can use to return a TextFrame object. You can determine if a shape supports the use of a text frame by using the Shape object's HasTextFrame property. Each TextFrame object has a HasText property you can use to determine if the text frame contains text.

The TextFrame object has a TextRange property you use to return a TextRange object. You use the TextFrame object's Text property to specify or determine the text within a frame. You use the properties and methods of the TextRange object to work with the text associated with a PowerPoint shape. To see an example of how to use these properties to add text to the text frame of a text box shape, see the AddTestSlideAndShapes procedure shown in "Adding Shapes to Slides," earlier in this chapter.

Note   Placeholder shapes contain default text that is visible from the PowerPoint user interface, but is not available programmatically. When you set the Text property of a Placeholder shape's TextRange object, the default text is replaced with the text you specify.

There is one Shape object that contains text but does not use the TextFrame or TextRange objects. The TextEffect property returns a TextEffectFormat object that contains the properties and methods used to work with WordArt shapes. You add WordArt shapes to a slide by using the Shapes collection's AddTextEffect method. The text of the WordArt shape and the location of the shape are specified in the arguments to the AddTextEffect method. You use the TextEffectFormat object's Text property to read or change the text in a WordArt shape. For example, the following code changes the text of an existing WordArt shape on the first slide of the current presentation:

With ActivePresentation
   strExistingText =    .Slides(1).TextEffect.Text
   If Len(strNewText) <= Len(strExistingText) Then
      .Slides(1).TextEffect.Text = strNewText
   End If
End With

Note that this code checks to make sure the new text is not longer than the existing text. This step is required because a WordArt shape does not automatically resize itself to accommodate new text. Alternatively, you could capture the properties of the existing WordArt shape, then delete it and replace it with a new WordArt shape that uses the same properties as the old shape.

To see an example that shows how to add WordArt shapes to a slide, see the AddTestSlideAndShapes procedure shown in "Adding Shapes to Slides" earlier in this chapter.