Dealing with Shape Objects

   

Dealing with Shape Objects

PowerPoint slides are really just a collection of objects: titles, text boxes, pictures, labels, lines, curves, and so on. In PowerPoint VBA, each of these items is a Shape object. Therefore, in order to get full slide control in your VBA procedures, you must know how to add, edit, format, and otherwise manipulate these objects. That's the goal of this section.

Specifying a Shape

You have to specify a Shape object before you can work with it. The techniques you use for this are similar to those I outlined earlier for Slide objects.

For a single shape, use the Shapes object, which is the collection of all Shape objects on a particular slide. To specify a shape, either use the shape's index number (where 1 represents the first shape added to the slide, 2 is the second shape, and so on), or enclose the shape name in quotation marks. For example, if Rectangle 1 is the first shape, the following two statements would be equivalent:

 ActivePresentation.Shapes(1) ActivePresentation.Shapes("Rectangle 1") 

tip

graphics/tip_icon.gif

To work with every shape in the slide, use the Range method without an argument:

 Presentations(1). Slides(1).Shapes.Range 

If you need to work with multiple shapes, use the Range method of the Shapes object:

  Slide  .Shapes.Range(  Index  ) 

Slide

The Slide object that contains the shapes.

Index

An array that specifies the shapes.

As with multiple slides, use VBA's Array function for the Index argument, like so:

 Presentations(1).Slides(1).Shapes.Range(Array("Oval 1","TextBox 2")) 

Adding Shapes to a Slide

The Slides object has 14 different methods you can use to insert shapes into a slide. Many of these methods use similar arguments, so before listing the methods , let's take a quick tour of the common arguments:

BeginX

For connectors and lines, the distance (in points) from the shape's starting point to the left edge of the slide window.

BeginY

For connectors and lines, the distance (in points) from the shape's starting point to the top edge of the slide window.

EndX

For connectors and lines, the distance (in points) from the shape's ending point to the left edge of the slide window.

EndY

For connectors and lines, the distance (in points) from the shape's ending point to the top edge of the slide window.

FileName

The path and name of the file used to create the shape (such as a picture or an OLE object).

Height

The height of the shape (in points).

Left

The distance (in points) of the left edge of the shape from the left edge of the slide window.

Orientation

The orientation of text within a label or text box. For horizontal text use the constant msoTextOrientationHorizontal ; for vertical text use the constant msoTextOrientationVerticalFarEast .

SafeArrayOfPoints

For curves and polylines, this is an array of coordinate pairs that specifies the vertices and control points for the object.

Top

The distance (in points) of the top edge of the shape from the top edge of the slide window.

Width

The width of the shape (in points).

Here's a list of the Shapes object methods and arguments that you can use to create shapes:

Slide .Shapes.AddComment Adds a comment to the specified Slide using the following syntax:

  Slide  .Shapes.AddComment(  Left, Top, Width, Height  ) 

Slide .Shapes.AddConnector Adds a connector to the specified Slide using the following syntax:

  Slide  .Shapes.AddConnector(  Type, BeginX, BeginY, EndX, EndY  ) 

Type

A constant that specifies the connector type:

 

Type

Connector

 

msoConnectorCurve

A curved connector

 

msoConnectorElbow

A connector with an elbow

 

msoConnectorStraight

A straight connector

Slide .Shapes.AddCurve Adds a curved line to the specified Slide using the following syntax:

  Slide  .Shapes.AddCurve(  SafeArrayOfPoints  ) 
graphics/note_icon.gif

The AddConnector method returns a Shape object that represents the new connector. You use this object's ConnectorFormat property to set up the beginning and ending points of the connector. In other words, you use the ConnectorFormat.BeginConnect and ConnectorFormat.EndConnect methods to specify the shapes attached to the connector.


Slide .Shapes.AddLabel Adds a label to the specified Slide using the following syntax:

  Slide  .Shapes.AddLabel(  Orientation, Left, Top, Width, Height  ) 

Slide .Shapes.AddLine Adds a straight line to the specified Slide using the following syntax:

  Slide  .Shapes.AddLine(  BeginX, BeginY, EndX, EndY  ) 

Slide .Shapes.AddMediaObject Adds a multimedia file to the specified Slide using the following syntax:

  Slide  .Shapes.AddMediaObject(  FileName, Left, Top, Width, Height  ) 

Slide .Shapes.AddPicture Adds a graphic to the specified Slide using the following syntax:

  Slide  .Shapes.AddPicture(  FileName, LinkToFile, SaveWithDocument, Left, Top, Width, Height  ) 

Here's a summary of the extra arguments used in this method:

LinkToFile

Set this argument to True to set up a link to the original file. If this argument is False, an independent copy of the picture is stored in the slide.

SaveWithDocument

Set this argument to True to save the picture with the presentation. Note that this argument must be True if LinkToFile is False.

graphics/note_icon.gif

I'll show you how to add text to a label and text box when we look at Shape object properties (see "Some Shape Object Properties").


Slide .Shapes.AddPolyline Adds an open polyline or a closed polygon to the specified Slide using the following syntax:

  Slide  .Shapes.AddPolyline(  SafeArrayOfPoints  ) 

Slide .Shapes.AddShape Adds an AutoShape to the specified Slide using the following syntax:

  Slide  .Shapes.AddShape(  Type, Left, Top, Width, Height  ) 

Here, the Type argument is a constant that specifies the AutoShape you want to add. PowerPoint VBA defines dozens of these constants. To see the full list, look up the AutoShapeType property in the VBA Help system.

Slide .Shapes.AddTextbox Adds a text box to the specified Slide using the following syntax:

  Slide  .Shapes.AddTextbox(  Left, Top, Width, Height  ) 

Slide .Shapes.AddTextEffect Adds a WordArt text effect to the specified Slide using the following syntax:

  Slide  .Shapes.AddTextEffect(  PresetTextEffect, Text, FontName, FontSize, FontBold, graphics/ccc.gif FontItalic, Left, Top  ) 

Here's a summary of the extra arguments used in this method:

PresetTextEffect

A constant that specifies one of WordArt's preset text effects. Look up this method in the VBA Help system to see the few dozen constants that are available.

Text

The WordArt text.

FontName

The font applied to Text .

FontSize

The font size applied to Text .

FontBold

Set to True to apply bold to Text .

FontItalic

Set to True to apply italics to Text .

Slide .Shapes.AddTitle Adds a title to the specified Slide . This method takes no arguments. However, be aware that the AddTitle method will raise an error if the slide already has a title. To check in advance, use the HasTitle property, as shown in the following example:

 With ActivePresentation.Slides(1).Shapes      If Not .HasTitle Then         .AddTitle.TextFrame.TextRange.Text = "New Title"     End If End With 

Some Shape Object Properties

PowerPoint VBA comes equipped with more than three dozen Shape object properties that control characteristics such as the dimensions and position of a shape, whether or not a shape displays a shadow, and the shape name. Let's take a quick look at a few of these properties:

Shape .AnimationSettings This property returns an AnimationSettings object that represents the animation effects applied to the specified Shape . AnimationSettings contains various properties that apply special effects to the shape. Here's a sampler (see the VBA Help system for the complete list as well as the numerous constants that work with these properties):

  • AdvanceMode A constant that determines how the animation advances. There are two choicesautomatically (in other words, after a preset amount of time; use ppAdvanceOnTime ), or when the user clicks the slide (use ppAdvanceOnClick ). For the latter, you can specify the amount of time by using the AdvanceTime property.

  • AfterEffect A constant that determines how the shape appears after the animation is complete.

  • Animate A Boolean value that turns the shape's animation on (True) or off (False).

  • AnimateTextInReverse When this Boolean value is True, PowerPoint builds the text animation in reverse order. For example, if the shape is a series of bullet points and this property is True, the animation displays the bullet points from last to first.

  • EntryEffect A constant that determines the special effect applied initially to the shape's animation. For example, you can make the shape fade in by using the ppEffectFade constant.

  • TextLevelEffect A constant that determines the paragraph level that gets animated.

  • TextUnitEffect A constant that determines how PowerPoint animates text: by paragraph, by word, or by letter.

Shape .AutoShapeType For an AutoShape object, this property returns or sets the shape type for the specified Shape .

Shape .Fill This property returns a FillFormat object that represents the fill formatting for the specified Shape . The FillFormat object defines numerous methods you can wield to apply a fill to a shape:

  • Background Sets the fill to match the slide's background.

  • OneColorGradient Sets the fill to a one- color gradient.

  • Patterned Sets the fill to a pattern.

  • PresetGradient A constant that sets the fill to one of PowerPoint's preset gradients.

    graphics/note_icon.gif

    PowerPoint's color properties (such as ForeColor ) return a ColorFormat object. This object represents either the color of a one-color object or the background or foreground color of an object with a pattern or gradient. To set a color, use the ColorFormat object's RGB property and VBA's RGB function to set a red-green-blue value, as in this example:

     Shapes(1).Fill.Solid.ForeColor. RGB = RGB(255,0,0) 

  • PresetTextured A constant that sets the fill to one of PowerPoint's preset textures.

  • Solid Sets the fill to a solid color. After running this method, use the Fill.ForeColor property to set the fill color.

  • TwoColorGradient Sets the fill to a two-color gradient.

  • UserPicture Sets the fill to a graphics file that you specify.

  • UserTexture Sets the fill to a specified graphics image that gets tiled to cover the entire shape.

Shape .HasTextFrame A Boolean value that tells you if the specified Shape has a text frame (True) or not (False). See the TextFrame property, discussed later.

Shape .Height Returns or sets the height, in points, for the specified Shape .

Shape .Left Returns or sets the distance, in points, between the left edge of the bounding box of the specified Shape and the left edge of the presentation window.

Shape .Name This property returns or sets the name for the specified Shape .

Shape .Shadow This property returns a ShadowFormat object that represents the shadow for the specified Shape . The ShadowFormat object contains various properties that control the look of the shadow. For example, Shadow.ForeColor controls the shadow color and Shadow.Visible is a Boolean value that turns the shadow on (True) or off (False).

Shape .TextEffectFormat For a WordArt object, this property returns a TextEffectFormat object that represents the text effects of the specified Shape .

Shape .TextFrame This property returns a TextFrame object for the specified Shape . A text frame is an area within a shape that can hold text. The frame's text, as a whole, is represented by the TextRange object, and the actual text is given by the Text property of the TextRange object. This rather convoluted state of affairs means that you need to use the following property to a refer to a shape's text:

  Shape  .TextFrame.TextRange.Text 

For example, the following statements add to the active presentation a new slide that contains only a title, and then they set the title text to 2004 Budget Proposal:

 With ActivePresentation.Slides     With .Add(1, ppLayoutTitleOnly).Shapes(1)         .TextFrame.TextRange.Text = "2004 Budget Proposal"     End With End With 

Also note that the TextFrame object has a number of other properties that control the text margins, orientation, word wrap, and more.

Shape .Top Returns or sets the distance, in points, between the top edge of the bounding box of the specified Shape and the top edge of the presentation window.

Shape .Visible A Boolean value that makes the specified Shape either visible (True) or invisible (False).

Shape .Width Returns or sets the width, in points, for the specified Shape .

The Juggling Application: Creating the Title Page

To put some of these properties through their paces, Listing 9.4 shows the Juggling application's SetUpStartPage procedure.

Listing 9.4. A Procedure That Sets Up the Text and Animation Settings for the First Page of the Juggling Presentation
 Sub SetUpStartPage()     Dim shapeTitle As Shape     Dim shapeSubTitle As Shape     With pres.Slides("Opener")         Set shapeTitle = .Shapes(1)     ' The title         Set shapeSubTitle = .Shapes(2)  ' The subtitle         '         ' Add the title text         '         With shapeTitle.TextFrame.TextRange             .Text = "Juggling"             With .Font                 .Name = "Arial"                 .Size = 44                 .Bold = True                 .Color.RGB = RGB(255, 255, 255)             End With         End With         '         ' Set the title animation         '         With shapeTitle.AnimationSettings             .Animate = True             .AdvanceMode = ppAdvanceOnTime             .AdvanceTime = 0             .TextUnitEffect = ppAnimateByCharacter             .EntryEffect = ppEffectFlyFromLeft         End With         '         ' Add the subtitle text         '         With shapeSubTitle.TextFrame.TextRange             .Text = "A Step-By-Step Course"             With .Font                 .Name = "Arial"                 .Size = 36                 .Bold = True                 .Color.RGB = RGB(255, 255, 255)             End With         End With         '         ' Set the subtitle animation         '         With shapeSubTitle.AnimationSettings             .Animate = True             .AdvanceMode = ppAdvanceOnTime             .AdvanceTime = 0             .TextUnitEffect = ppAnimateByWord             .EntryEffect = ppEffectFlyFromBottom         End With     End With End Sub 

The first slide is named Opener, and this is the object used through most of the procedure. The shapeTitle variable is Set to the slide's titleShapes(1)and the shapeSubTitle variable is Set to the subtitle text boxShapes(2).

From there, the title's TextFrame property is used to add and format the title text. Then its AnimationSettings property is used to animate the text. A similar sequence of code adds text, formatting, and animation to the subtitle.

Some Shape Object Methods

The Shape object comes with a number of methods that let you perform standard actions such as cutting, copying, pasting, and deleting. Here's a list of some other useful methods:

Shape .Apply This method applies to the specified Shape the formatting that was captured from another shape using the PickUp method (described later).

Shape .Duplicate This method makes a copy of the specified Shape in the same slide. The new shape is added to the Shapes object immediately after the specified Shape . Note, too, that this method returns a Shape object that refers to the new shape.

Shape .Flip This method flips the specified Shape around its horizontal or vertical axis. Here's the syntax:

  Shape  .Flip(  FlipCmd  ) 

Shape

The Shape object you want to flip.

FlipCmd

A constant that determines how the shape is flipped . Use either msoFlipHorizontal or msoFlipVertical .

Shape .IncrementLeft Moves the specified Shape horizontally using the following syntax:

  Shape  .IncrementLeft(  Increment  ) 

Shape

The Shape object you want to move.

Increment

The distance, in points, that you want the shape moved. Use a positive number to move the shape to the right; use a negative number to move the shape to the left.

Shape .IncrementRotation Rotates the specified Shape around its z-axis using the following syntax:

  Shape  .IncrementRotation(  Increment  ) 

Shape

The Shape object you want to move.

Increment

The number of degrees you want the shape rotated . Use a positive number to rotate the shape clockwise; use a negative number to rotate the shape counterclockwise.

Shape .IncrementTop Moves the specified Shape vertically using the following syntax:

  Shape  .IncrementTop(  Increment  ) 

Shape

The Shape object you want to move.

Increment

The distance, in points, that you want the shape moved. Use a positive number to move the shape down; use a negative number to move the shape up.

Shape .PickUp Copies the formatting of the specified Shape . Use the Apply method (discussed earlier) to apply the copied formatting to a different object.

Shape .Select This method selects the specified Shape using the following syntax:

  Shape  .Select(  Replace  ) 

Shape

The Shape object you want to select.

Replace

A Boolean value that either adds the shape to the current selection (False) or replaces the current selection (True). True is the default.

The Juggling Application: Creating the Instructions

To continue the Juggling application, the SetUpJugglingPages procedure, shown in Listing 9.5, is run. This procedure serves to set up the title, picture, and instruction text for each of the four instruction slides.

Listing 9.5. A Procedure That Sets Up the Titles, Pictures, and Text Instructions for Each of the Juggling Slides
 Sub SetUpJugglingPages()     Dim thisPres As Presentation     Dim slideTitle As Shape     Dim slidePicture As Shape     Dim slideText As Shape     Dim i As Integer     For i = 1 To 4         With pres.Slides("Juggling" & i)             '             ' Get pictures from Chaptr09.ppt             '             Set thisPres = Presentations("Chaptr09.ppt")             thisPres.Slides(1).Shapes(i + 1).Copy             .Shapes.Paste             '             ' Adjust the layout and then set the Shape variables             '             .Layout = ppLayoutObjectOverText             Set slideTitle = .Shapes(1)             Set slidePicture = .Shapes(2)             Set slideText = .Shapes(3)             '             ' Add the title text             '             With slideTitle.TextFrame.TextRange                 Select Case i                     Case 1                         .Text = "Step 1: The Home Position"                     Case 2                         .Text = "Step 2: The First Throw"                     Case 3                         .Text = "Step 3: The Second Throw"                     Case 4                         .Text = "Step 4: The Third Throw"                 End Select                 With .Font                     .Name = "Arial"                     .Size = 44                     .Bold = True                     .Color.RGB = RGB(255, 255, 255)                 End With             End With             '             ' Set the picture animation and shadow             '             With slidePicture                 With .AnimationSettings                     .Animate = True                     .AdvanceMode = ppAdvanceOnTime                     .AdvanceTime = 0                     .EntryEffect = ppEffectFade                 End With                 With .Shadow                     .ForeColor.RGB = RGB(0, 0, 0)                     .OffsetX = 10                     .OffsetY = 10                     .Visible = True                 End With             End With             '             ' Add the instruction text             '             With slideText.TextFrame.TextRange                 Select Case i                 Case 1                 .Text = "Place two balls in your dominant hand, " & _                     "one in front of the other." & Chr(13) & _                     "Hold the third ball in your other hand." & _                     Chr(13) & _                     "Let your arms dangle naturally and bring " & _                     "your forearms parallel to the ground (as " & _                     "though you were holding a tray.)" & Chr(13) & _                     "Relax your shoulders, arms, and hands."                 Case 2                 .Text = "Of the two balls in your dominant hand, " & _                     "toss the front one towards your other hand " & _                     "in a smooth arc." & Chr(13) & _                     "Make sure the ball doesn't spin too much." & _                     Chr(13) & _                     "Make sure the ball goes no higher than " & _                     "about eye level."                 Case 3                 .Text = "Once the first ball reaches the top of " & _                     "its arc, toss the ball in your other hand." & _                     Chr(13) & _                     "Throw the ball towards your dominant hand, " & _                     "making sure it flies UNDER the first ball." & _                     Chr(13) & _                     "Again, try not to spin the ball and make " & _                     "sure it goes no higher than eye level."                 Case 4                 .Text = "Now for the tricky part (!). Soon " & _                     "after you release the second ball, the " & _                     "first ball will approach your hand. Go " & _                     "ahead and catch the first ball." & Chr(13) & _                     "When the second ball reaches its apex, " & _                     "throw the third ball (the remaining ball " & _                     "in your dominant hand) under it." & Chr(13) & _                     "At this point, it just becomes a game of " & _                     "catch-and-throw-under, catch-and-throw-" & _                     "under. Have fun!"                 End Select                 With .Font                     .Name = "Times New Roman"                     .Size = 24                     .Bold = False                     .Color.RGB = RGB(255, 255, 255)                 End With             End With         End With     Next i End Sub 

A For...Next loop runs through each of the four instructional slides. (Recall that earlier, the CreateJugglingSlides procedure gave these slides the names Juggle1 through Juggle4.) Here's a summary of the various chores that are run within this loop:

  • The first task is to load the pictures that illustrate each step. These pictures can be found on the slide in Chaptr09.ppt . To get them into the Juggling presentation, the code uses the Copy method to copy each one from Chaptr09.ppt to the Clipboard, and then it uses the Paste method to add the picture to the Juggling slide. When that's done, the slide's Layout property is set to ppLayoutObjectOverText , and the three variables that represent the three shapes on each slide are Set.

  • Next, the title text is added. Here, a Select Case structure is used to add a different title to each slide, and then the text is formatted.

  • The picture is animated, and a shadow is added.

  • The last chunk of code uses another Select Case to add the appropriate instructions for each slide, and then the instruction text is formatted.



Absolute Beginner's Guide to VBA
Absolute Beginners Guide to VBA
ISBN: 0789730766
EAN: 2147483647
Year: 2003
Pages: 146

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net