In the previous chapter, you learned to work with Presentation
objects, Slide
objects, and Master
objects. In this chapter, you'll learn to work with Shape
objects to manipulate the contents of slides and with HeaderFooter
objects to control the contents of headers and footers. You'll also see how to set up and run a slide show using VBA.
In this chapter you will learn to do the following:
Most of the objects on a typical PowerPoint slide are Shape
objects. For example, a title box is a Shape
object, as is a picture or a Word table that you've pasted in. You access the Shape
objects through the Shapes
collection of a Slide
object, a SlideRange
object, or a Master
object.
The Shapes
collection uses different methods for adding the different types of shapes. Table 25.1 lists the Shape
objects you can add and the methods and arguments for adding them. The following sections explain the arguments.
These are the arguments that are shared among various shape-adding methods:
BeginX
and EndX
are required arguments (of the Single data type) that specify the horizontal starting position and ending position of the connector or line, measured in points from the left edge of the slide.
BeginY
and EndY
are required Single data arguments that specify the vertical starting point and ending point of the connector or line, measured in points from the top of the slide.
FileName
is a required String argument used to specify the file to be used for creating the object (for example, the media file for creating a media object).
Left
is a required Single argument that specifies the position of the left edge of the shape from the left edge of the slide, measured in points. Top
is a required Single argument that specifies the position of the top edge of the shape from the top edge of the slide, measured in points.
Height
is a required Single argument that specifies the height of the shape, measured in points. Width
is a required Single argument that specifies the width of the shape, measured in points.
LinkToFile
is on optional argument that you can set to msoTrue
to link the picture to its source file.
NumColumns
and NumRows
are required Long arguments that specify the number of columns and rows in the table you're adding.
Orientation
is a required argument that specifies the orientation: msoTextOrientationHorizontal
(horizontal) or msoTextOrientationVerticalFarEast
(vertical).
SafeArrayOfPoints
is a required Variant argument that supplies an array of coordinate pairs that give the vertices and control points of a curve or polyline. The line begins at the first pair of coordinates and ends at the last pair.
SaveWithDocument
is a required argument that controls whether PowerPoint saves the linked picture in the presentation (msoTrue
) or not (msoFalse
). If you set LinkToFile: =msoFalse
, you must set SaveWithDocument: =msoTrue
.
Table 25.1. Shapes and the methods for adding them to slides
Use This Method and These Arguments | |
---|---|
Callout |
|
Chart |
|
Comment |
|
Connector |
|
Curve |
|
Label |
|
Line |
|
Media object |
|
Media object 2 |
|
Media object from embed tag |
|
OLE object |
|
Picture |
|
Placeholder |
|
Polyline |
|
Shape |
|
Smart Art |
|
Table |
|
Textbox |
|
Title |
|
The Type
argument is different for the various methods that use it:
Type
for the AddPlaceholder
method is a required argument that specifies the type of placeholder to add. The names are self-explanatory: ppPlaceholderBitmap, ppPlaceholderBody, ppPlaceholderCenterTitle, ppPlaceholderChart, ppPlaceholderDate, ppPlaceholderFooter, ppPlaceholderHeader, ppPlaceholderMediaClip, ppPlaceholderObject, ppPlaceholderOrgChart, ppPlaceholderSlideNumber, ppPlaceholderSubtitle, ppPlaceholderTable, ppPlaceholderTitle, ppPlaceholderVerticalBody
, or ppPlaceholderVerticalTitle
.
Type
for the AddCallout
method is a required argument that specifies the type of callout line to add: msoCalloutOne
(a one-segment line that can be vertical or horizontal), msoCalloutTwo
(a one-segment line that rotates freely), msoCalloutThree
(a two-segment line), or msoCalloutFour
(a three-segment line).
Type
for the AddShape
method is a required argument that specifies the type of AutoShape to add. There are too many constants to list here, but most are easy to identify from their names. For example, msoShapeHeart
is a heart shape, msoShapeLightningBolt
gives a lightning bolt, and so on. To see a list of the constants, search for the AddShape
method in the VBA Editor Help file, and then click the link for the msoAutoShapeType
entry. Or type msoautoshapetype in the editor's Object Browser search field.
Type
for the AddDiagram
method is a required argument that specifies the diagram type: msoDiagramCycle
(a cycle diagram), msoDiagramOrgChart
(an org chart), msoDiagramPyramid
(a pyramid diagram), msoDiagramRadial
(a radial diagram), msoDiagramTarget
(a target diagram), or msoDiagramVenn
(a Venn diagram).
The following arguments apply only to the AddTextEffect
method:
PresetTextEffect
is a required argument that specifies the preset text effect to use. These preset text effects are identified by the constants msoTextEffect1
through msoTextEffect30
, which correspond to the order in which the samples appear in the WordArt Gallery dialog box (1 through 6 are the first row, 7 through 12 the second row, and so on).
Text
is a required String argument that specifies the text to use in the WordArt object.
FontBold
is a required argument that you set to msoTrue
to make the font bold or msoFalse
to make it not bold.
FontItalic
is a required argument that you set to msoTrue
to make the font italic and msoFalse
to make it not italic.
FontName
is a required String argument that specifies the name of the font to use.
FontSize
is a required Single argument that specifies the font size to use.
The following arguments apply only to the AddOLEObject
method:
ClassName
is an optional String argument that specifies the program ID (the ProgID) or OLE long class name for the object. You must use either ClassName
or FileName
, but not both. In most cases, it's easiest to use FileName
.
DisplayAsIcon
is an optional argument that you can set to msoTrue
to display the OLE object as an icon rather than as itself (the default).
IconFileName
is an optional String argument that you can use with DisplayAsIcon:=True
to specify the filename of the icon you want to display for the object.
IconIndex
is an optional Integer argument that specifies the index of the icon to use within the icon file specified by IconFileName
. If you omit the IconIndex
argument, VBA uses the second icon in the icon file, the icon at position 1 (the first icon in the file is at position 0).
IconLabel
is an optional String argument that you can use to specify the caption (or label) to display under the icon.
Link
is an optional argument that you can set to msoTrue
to link the OLE object to its source file when you use the FileName
argument. Link
must be msoFalse
when you use ClassName
to specify a class name.
The following statement uses the AddShape
method to add a bent-up-arrow to the upper-right corner of the penultimate slide in the active presentation. Before executing this example, click the File tab on PowerPoint's Ribbon, then click the New option in the left pane to see the available templates and themes. Open the Sample Templates folder and double-click one of the templates so you'll have some slides to work with in this example.
Open the Visual Basic Editor by pressing Alt+F11. Insert a new module from the Visual Basic Editor's Insert menu. Type the following into the new module, and then press F5 with your cursor inside this subroutine to execute the code and see the effect:
Sub test() ActivePresentation.Slides(ActivePresentation.Slides.Count - 1) _ .Shapes.AddShape Type:=msoShapeBentUpArrow, Left:=575, Top:=10, _ Width:=150, Height:=75 End Sub
To see what happened, scroll down in PowerPoint to the next-to-last slide (Slides.Count – 1
means the total number of slides minus one). Then look at that slide and notice that a shape has been added to it—a bent up-arrow.
The following example uses the AddTextEffect
method to superimpose a WordArt item onto the third slide. It draws the text Questions & Answers (on three lines) on it. The WordArt item uses 54-point bold Garamond.
ActivePresentation.Slides(3).Shapes.AddTextEffect
_PresetTextEffect:=msoTextEffect14
, _ Text:="Questions" + Chr$(CharCode:=13) + _ "&" + Chr$(CharCode:=13) + "Answers", _ FontName:="Garamond", FontSize:=54, FontBold:=msoTrue, _ FontItalic:=msoFalse, Left:=230, Top:=125
There are 30 msoTextEffect
constants you can experiment with. msoTextEffect14
is nice; it provides a kind of metallic effect.
The following example adds a text box to the second slide in the active presentation and assigns text to it:
Dim myTextBox As Shape With ActivePresentation.Slides(2) Set myTextBox = .Shapes.AddTextbox _ (Orientation:=msoTextOrientationHorizontal, Left:=100, Top:=50, _ Width:=400, Height:=100) myTextBox.TextFrame.TextRange.Text = "Corrective Lenses" End With
To delete a shape, use the Delete
method with the appropriate Shape
object. For example, the following statement deletes the first Shape
object on the second slide in the active presentation:
ActivePresentation.Slides(2).Shapes(1).Delete
To select all the shapes on a slide, use the SelectAll
method of the appropriate Shapes
collection. For example, the following statement selects all the Shape
objects on the first slide in the active presentation:
ActivePresentation.Slides(1).Shapes.SelectAll
To reposition a shape, set its Left
property (to specify the distance in points from the left edge of the slide to the left edge of the shape) and its Top
property (to specify the distance in points from the top edge of the slide to the top edge of the shape).
To change the size of a shape, set its Width
and Height
properties to the appropriate number of points.
For example, the following statements position the first shape on the first slide in the active presentation 200 points from the left of the slide and 100 points from its top and make the shape 300 points wide by 200 points high:
With ActivePresentation.Slides(1).Shapes(1) .Left = 200 .Top = 100 .Width = 300 .Height = 200 End With
You can also move a shape by using the IncrementLeft
method and the IncrementTop
method and rotate the shape by using the IncrementRotation
method. These moves are relative to the current position of the shape, not absolute specified locations within a slide. Each of these methods takes an Increment
argument:
For the IncrementLeft
and IncrementTop
methods, the Increment
argument specifies the number of points to move the shape. A negative number moves the shape to the left or upward, while a positive number moves the shape to the right or downward.
For the IncrementRotation
method, the Increment
argument specifies the number of degrees to rotate the shape. A positive number rotates the shape clockwise; a negative number rotates the shape counterclockwise.
The following example works with the first shape on the third slide of the active presentation, moving it 100 points to the left and 200 points down and rotating it 90 degrees counterclockwise:
With ActivePresentation.Slides(3).Shapes(1) .IncrementLeft Increment:=-100 .IncrementTop Increment:=200 .IncrementRotation Increment:=-90 End With
Often, it's useful to be able to apply the same formatting to multiple shapes. When one shape has the formatting you need, you can use the PickUp
method of the Shape
object to copy the formatting from that shape and then use the Apply
method to apply that formatting to another shape.
Neither the PickUp
method nor the Apply
method uses any arguments. The following example copies the formatting from the first shape on the second slide in the active presentation and applies it to the third shape on the fourth slide:
With ActivePresentation .Slides(2).Shapes(1).PickUp .Slides(4).Shapes(3).Apply End With
The text within a shape is contained in a TextRange
object, which itself is contained in a TextFrame
object. To work with the text in a shape, you use the TextFrame
property of the Shape
object to return the TextFrame
object and then use the TextRange
property of the TextFrame
object to return the TextRange
object. Got it?
Within the TextRange
object, the Text
property contains the text, the Font
object contains the font formatting, the ParagraphFormat
object contains the paragraph formatting, and the ActionSettings
collection contains the action settings for the text range.
Not every shape has a text frame, so it's a good idea to first determine that the shape in which you're intending to manipulate text in fact even has a text frame.
To do so, check that the HasTextFrame
property of the Shape
object is msoTrue
, as in this example:
If ActivePresentation.Slides(1).Shapes(1).HasTextFrame = msoTrue Then MsgBox "The shape contains a text frame." End If
You may also need to check whether the text frame contains text. To do so, check that the HasText
property of the TextFrame
object is msoTrue
. Here's an example:
With ActivePresentation.Slides(1).Shapes(1).TextFrame If .HasText = msoTrue Then MsgBox .TextRange.Text End With
To return (read) or set (specify) the text in a text range, you can simply use the Text
property of the TextRange
object. For example, the following statement sets the text in the first shape on the fourth slide in the presentation identified by the object variable myPresentation
to Strategic Planning Meeting
:
Sub Test()
Dim myPresentation As Presentation
Set myPresentation = Presentations(1)
myPresentation.Slides(4).Shapes(1).TextFrame.TextRange.Text
_
= "Strategic Planning Meeting"
End Sub
You can also return parts of the text by using the Paragraphs
method, the Sentences
method, the Lines
method, the Words
method, the Characters
method, or the Runs
method. The syntax for these methods is shown here, using the Paragraphs
method as the example:
expression
.Paragraphs(Start, Length)
The components of the syntax are as follows:
expression is a required expression that returns a TextRange
object.
Start
is an optional Long argument that specifies the first item (paragraph, sentence, line, word, character, or text run) to return.
Length
is an optional Long argument that specifies how many items to return—for example, two paragraphs, three sentences, or four words.
The following code example returns the second through fifth words (the four words starting with the second word) from the first shape on the first slide in the active presentation:
MsgBox ActivePresentation.Slides(1).Shapes(1).TextFrame _ .TextRange.Words(Start:=2, Length:=4)
The next code example sets the text of the second paragraph in the second shape on the sixth slide in the presentation identified by the object variable myPresentation
to VP of Business Development
:
myPresentation.Slides(6).Shapes(2).TextFrame.TextRange _ .Paragraphs(Start:=2, Length:=1).Text = "VP of Business Development"
To format the text in a text range, use the ParagraphFormat
object to control the paragraph formatting (including the alignment and the space before and after) and the Font
object to control the font formatting.
These are the most useful properties of the ParagraphFormat
object:
The Alignment
property controls the alignment. Use ppAlignLeft
for left alignment, ppAlignCenter
for centering, ppAlignJustify
for justified alignment, ppAlignDistribute
for distributed alignment (justified using all available space), or ppAlignRight
for right alignment.
The Bullet
property returns the BulletFormat
object, which represents the bullet formatting. See the next section for details.
The LineRuleBefore
property, the LineRuleAfter
property, and the LineRuleWithin
property determine whether the measurements set by the SpaceBefore
property, the SpaceAfter
property, and the SpaceWithin
property use lines (msoTrue
) or points (msoFalse
).
The SpaceBefore
property and the SpaceAfter
property control the amount of space before and after each paragraph. The SpaceWithin
property controls the amount of space between base lines in a paragraph. All measurements are in points.
The following example sets left alignment, 18 points of spacing before and after paragraphs, and 12 points of spacing between lines for the second shape on the slide identified by the object variable mySlide
:
Dim mySlide As Slide Set mySlide = Presentations(1).Slides(2)
With mySlide.Shapes(2).TextFrame.TextRange.ParagraphFormat .Alignment = ppAlignLeft .LineRuleAfter = msoFalse .SpaceAfter = 18 .LineRuleBefore = msoFalse .SpaceBefore = 18 .LineRuleWithin = msoFalse .SpaceWithin = 12 End With
Bullets and numbers are vital to the lists used in many PowerPoint slides. To control whether and how bullets and numbers appear, use the Bullet
property of the TextRange
object to return the BulletFormat
object, and then work with the BulletFormat
object's properties and methods.
To make bullets and numbers visible, set the Visible
property of the BulletFormat
object to msoTrue
; to hide bullets and numbers, set Visible
to msoFalse
.
To specify which type of bullet or numbering to use, set the Type
property of the BulletFormat
object to ppBulletUnnumbered
(for a bullet), ppBulletNumbered
(numbers), ppBulletPicture
(for a picture), or ppBulletNone
(no bullet).
To specify the bullet character, use the Character
property and the character number. You can find out the character number from the Symbol dialog box or the Character Map applet, which you can run by choosing Start
Unfortunately, the character codes are given in the hexadecimal numbering system. If you look up the check box symbol for the Wingdings font that's used in the following code example, the character map utility doesn't say 254 in our human decimal numbering system. Instead, it says Character Code: 0xFE. This tedious holdover from the early days of computing serves no particular purpose in character codes, but you have to deal with it. Why? Because some people think that pointless complexity is cute, or it helps make programming seem somehow more mysterious than it in fact is.
To solve the hex character code problem, you can either use a calculator that can translate between hex and decimal or prepend the characters &H in front of the hex code and let VBA translate it for you when executing your procedure. For example, in the following code example, I used 254 (a decimal number) because I can translate hex. But if you can't, or more likely don't want to be bothered, just click the character you want to use in the Character Map dialog box and then look at its hex code in the lower-left portion. In this example, it's listed as 0xFE (which means, you guessed it, decimal 254). Since the Wingdings font has only 256 characters, ignore the 0x part and use the FE, like this, in your code:
.Character = &H FE
Use the Font
property to specify the font name, size, and color. The following example sets the bullet for the first shape on the slide identified by the object variable mySlide
to Wingdings character 254, a check box, using the color white, which is RGB(255, 255, 255)
, and 44-point size:
With mySlide.Shapes(1).TextFrame.TextRange.ParagraphFormat.Bullet .Type = ppBulletUnnumbered .Character = 254 With .Font .Name = "Wingdings" .Size = 44 .Color = RGB(255, 255, 255) End With End With
Color is of course an important element in any design. You can easily find out which RGB values you need to employ for various colors by visiting this web page:
http://cloford.com/resources/colours/500col.htm
To use your own custom picture as a bullet, set the Type
property of the BulletFormat
object to ppBulletPicture
and then use the Picture
method with the Picture
argument, a required String argument that specifies the path and filename of the file to use as the bullet. You can use most types of graphics files, including BMP, EPS, GIF, JPG, JPEG, PCX, PNG, TIFF, and WMF files. The following example uses the file Face1.jpg
stored in the folder Z:PublicPictures
as the bullet for the first shape on the slide identified by the object variable mySlide
:
With mySlide.Shapes(1).TextFrame.TextRange.ParagraphFormat.Bullet .Type = ppBulletPicture .Picture Picture:="z:PublicPicturesFace1.jpg" End With
To animate a shape or a range of shapes, use the AnimationSettings
property of the Shape
object or the ShapeRange
object to return the AnimationSettings
object.
To specify the animation effect to use, set the EntryEffect
property to the constant for the effect. Let's see how to figure out which animation effect looks best for the shape you're working with. First, click a shape in a slide to select the shape. Now display the Add Animation pane.
There are too many animation constants to list here, but their names are easy to work out from the names listed in the Add Animation pane. To open this pane, click the Animation tab in PowerPoint's Ribbon, then click the Add Animation icon in the Advanced Animation section. A pane drops down on the right side of PowerPoint's window. As you hover your mouse pointer over each animation option in the Add Animation pane, you'll see the effect illustrated visually in the slide. It's pretty cool, not to mention efficient.
To write code that creates an animation, set the Animate
property to msoTrue
. (To turn off an animation, set Animate
to msoFalse
.)
To control how the text in a shape is animated, set the TextLevelEffect
property to ppAnimateLevelNone
(no animation), ppAnimateByFirstLevel, ppAnimateBySecondLevel, ppAnimateByThirdLevel, ppAnimateByFourthLevel, ppAnimateByFifthLevel
, or ppAnimateByAllLevels
.
If you set TextLevelEffect
to any value other than ppAnimateByAllLevels
or ppAnimate LevelNone
, you can use the TextUnitEffect
property to specify how to animate the text. Use ppAnimateByParagraph
to animate by paragraph, ppAnimateByWord
to animate by word, or ppAnimateByCharacter
to animate by character.
To reverse the order of the animation, set the AnimateTextInReverse
property to msoTrue
. (The default is msoFalse
.)
To control how the animation advances, set the AdvanceMode
property to ppAdvanceOnTime
(for automatic advancing using a timing) or ppAdvanceOnClick
(for manual advancing). If you use automatic advancing, use the AdvanceTime
property to specify the number of seconds to wait before advancing.
To play a preset sound effect with the transition, use the SoundEffect
property of the AnimationSettings
object to return the SoundEffect
object, use the Name
property to specify the name of the sound effect, and then use the Play
method to play the sound effect. You can also play a separate sound file by using the ImportFromFile
method of the SoundEffect
object and using the FullName
argument to specify the path and filename of the sound file.
To control how a media clip is played, use the PlaySettings
property of the Animation Settings
object to return the PlaySettings
object. For example, if you want the sound to loop until the next sound, set the LoopSoundUntilNext
property of the PlaySettings
object within the AnimationSettings
object to msoTrue
. The default value is msoFalse
.
You can find all these options by pressing F2 to display the Object Browser in the VBA Editor and then searching for them. For example, search for ppEntryEffect
to see all possible constants for the various possible lead-in animations.
The following example applies a custom animation to the first shape on the slide identified by the object variable mySlide
. The animation uses the entry effect Fly In from the right, plays a sound effect from a file, animates the text by first-level paragraphs and by whole paragraphs, and advances when the user clicks:
Dim mySlide As Slide Set mySlide = Presentations(1).Slides(2) With mySlide.Shapes(1).AnimationSettings .EntryEffect = ppEffectFlyFromRight .AdvanceMode = ppAdvanceOnClick .SoundEffect.ImportFromFile FileName:="D:MediaWhistle4.wav" .TextLevelEffect = ppAnimateByFirstLevel .TextUnitEffect = ppAnimateByParagraph End With
To test this (or other code examples you try in PowerPoint), just press F5 in the main PowerPoint window, and then repeatedly click the screen to activate the various transitions and effects. Press Esc when you're done.
PowerPoint uses HeaderFooter
objects to represent the headers, footers, slide numbers, and date and time on slides. The HeaderFooter
objects are organized into the HeadersFooters
collection, which you access through the HeaderFooters
property of the Master
object, a Slide
object, or a SlideRange
collection.
Be warned: Before you can execute the following code examples, you must first add a footer to the slides in your active presentation. The code examples expect to modify an existing footer, not to create it. So, before executing these examples, follow these steps:
Click the File tab on PowerPoint's Ribbon.
Click the New option in the left pane to see the Available Templates and Themes.
Open the Sample Templates folder and double-click the Pitchbook template so you'll have some slides to work with in the example code.
However, Pitchbook has no footers, so before trying the following code examples, click the Insert tab on PowerPoint's Ribbon, and then in the Text area, click the Header And Footer button to open the Header And Footer dialog box. In this dialog box, click the Date And Time check box and the Footer check box. Then click the Apply To All button.
To return the object you want, use the appropriate property of the HeaderFooter
object:
Use the DateAndTime
property to return the date and time.
Use the Footer
property to return the footer.
Use the Header
property to return the header on a notes page or handout. Slides can't have a header.
Use the SlideNumber
property to return the slide number on a slide or the page number on a notes page or a handout.
The following example uses the Footer
property to set the text of the HeaderFooter
object of the first slide in the active presentation:
ActivePresentation.Slides(1).HeadersFooters.Footer.Text = "Sentence 102"
To display the HeaderFooter
object, set its Visible
property to msoTrue
(or just True
). To hide the HeaderFooter
object, set its Visible
property to msoFalse
. For example, the following statement hides the footer on the fifth slide in the active presentation:
ActivePresentation.Slides(5).HeadersFooters.Footer.Visible = False
To set the text that you want in a HeaderFooter
object, assign a string containing the text to the object's Text
property. For example, the following statement sets the text of the footer of the fifth slide in the active presentation to Confidential
:
ActivePresentation.Slides(5).HeadersFooters.Footer.Text = "Confidential"
If you executed the previous example code, executing this example will trigger an error message. That's because you made the same slide (#5) invisible in the previous code. To be able to set the text in this slide, it must first be visible:
ActivePresentation.Slides(5).HeadersFooters.Footer.Visible = True
ActivePresentation.Slides(5).HeadersFooters.Footer.Text = "Confidential"
If your slides, notes pages, or handouts use dates and times in their footers or headers, use the Format
property to specify how the dates and times should appear. Table 25.2 lists the constants you can use.
Table 25.2. Format
property constants for date and time headers and footers
Format | Example |
---|---|
| Thursday, October 05, 2008 |
| 5 October 2008 |
| 5-Oct-08 |
| 10:17 |
| 10:17AM |
| 10:17:16 |
| 10:17:16AM |
| 10/5/2008 |
| 10/5/2008 10:17AM |
| 10/5/2008 10:17:16AM |
| October 5, 2008 |
| October 08 |
| Oct-08 |
Set the UseFormat
property of the HeaderFooter
to msoTrue
if you want the date and time to be updated automatically. Set UseFormat
to msoFalse
if you want the date and time to remain unchanged.
The following sections show an example of using the UseFormat
property and the format
property of a HeaderFooter
object.
Not only can you assemble and format a slide show using VBA, you can also run it using VBA. To set up a slide show, use the SlideShowSettings
property of the Presentation
object to return the SlideShowSettings
object. When you run the slide show, VBA creates a SlideShowWindow
object, which you can then manipulate to control the slide show.
To specify the type of show, set the ShowType
property of the SlideShowSettings
object to ppShowTypeSpeaker
(for a standard full-screen presentation presented by a speaker), ppShowTypeKiosk
(for a kiosk presentation), or ppShowTypeWindow
(for a "browsed by an individual" presentation that appears in a window). For a show in a window, you can use the Left
and Top
properties to specify the position of the upper-left corner of the window and the Height
and Width
properties to specify its size.
To control whether animation and narration are used, set the ShowWithAnimation
property and the ShowWithNarration
property of the SlideShowSettings
object to msoTrue
or msoFalse
.
To control whether the presentation loops until stopped, set the LoopUntilStopped
property of the SlideShowSettings
object to msoTrue
or msoFalse
.
To control how the presentation advances, set the AdvanceMode
property to ppSlideShow ManualAdvance
(for manual advancing), ppSlideShowUseSlideTimings
(for automatic advancing using timings already set), or ppSlideShowRehearseNewTimings
(to rehearse new timings while the show plays).
The following example sets the active presentation running as a kiosk presentation that will advance automatically using its timings and loop until it is stopped:
With ActivePresentation.SlideShowSettings .LoopUntilStopped = msoCTrue .AdvanceMode = ppSlideShowUseSlideTimings .ShowType = ppShowTypeKiosk .Run End With
The following example sets the presentation named Corporate.pptm
running in speaker (full-screen) mode, sizing the image to 800×600 pixels and positioning it at the upper-left corner of the screen. The show uses manual advancing:
With Presentations("Corporate.pptm").SlideShowSettings .LoopUntilStopped = msoFalse .ShowType = ppShowTypeSpeaker .AdvanceMode = ppSlideShowManualAdvance With .Run
.Height = 600 .Width = 800 .Left = 0 .Top = 0 End With End With
Custom shows within a presentation are represented by the NamedSlideShows
collection within the SlideShowSettings
object. Use the NamedSlideShows
property of the SlideShowSettings
object to return the NamedSlideShows
collection.
To create a custom show, use the Add
method of the NamedSlideShows
collection. The syntax is as follows:
expression
.Add(Name, SafeArrayOfSlideIDs)
Here, expression is a required expression that returns a NamedSlideShows
object. Name
is a required String argument that specifies the name to assign to the new custom show. SafeArrayOfSlideIDs
is a required Variant that specifies the numbers or names of the slides to include in the custom show.
For example, the following statements declare an array of the Long data type; assign to it slides 2, 4, 5, and 10 from the open presentation named Corporate.pptm
; and create a new custom show named Short Show
using the array:
Dim myArray(4) As Long With Presentations("Corporate.pptm") myArray(1) = .Slides(2).SlideID myArray(2) = .Slides(4).SlideID myArray(3) = .Slides(5).SlideID myArray(4) = .Slides(10).SlideID .SlideShowSettings.NamedSlideShows.Add Name:="Short Show", _ safeArrayOfSlideIDs:=myArray End With
To delete a custom show, use the Delete
method with the appropriate NamedSlideShow
object. For example, the following statement deletes the custom show named Overview
from the active presentation:
ActivePresentation.SlideShowSettings.NamedSlideShows("Overview").Delete
To start a slide show using the whole presentation, use the Run
method of the SlideShowSettings
object. For example, the following statement starts the slide show running in the presentation identified by the object variable myPresentation
:
myPresentation.SlideShowSettings.Run
To show only a range of slides from a presentation, set the RangeType
property of the SlideShowSettings
object to ppShowSlideRange
, use the StartingSlide
property of the SlideShowSettings
object to specify the first slide and the EndingSlide
property to specify the last slide, and then use the Run
method to run the presentation. The following example shows slides 4 through 8 in the presentation named Corporate.pptm
:
With Presentations("Corporate.pptm").SlideShowSettings .RangeType = ppShowSlideRange .StartingSlide = 4 .EndingSlide = 8 .Run End With
To start running a custom show, set the RangeType
property of the SlideShowSettings
object to ppShowNamedSlideShow
, use the SlideShowName
property to specify the name of the custom show, and then use the Run
method to run the custom show. The following example shows the custom show named Short Show
in the active presentation:
With ActivePresentation.SlideShowSettings .RangeType = ppShowNamedSlideShow .SlideShowName = "Short Show" .Run End With
When you start a slide show, VBA creates a SlideShowWindow
object representing the object. You can access the SlideShowWindow
object either through the SlideShowWindows
collection (a creatable object that contains a SlideShowWindow
object for each open slide show) or through the SlideShowWindow
property of the Presentation
object. If you know which presentation is running, it's easier to go through the appropriate Presentation
object.
To find out whether a slide show is displayed full screen or in a window, check the IsFullScreen
property of the SlideShowWindow
object. If the IsFullScreen
property returns −1
, the presentation is full screen; if the property returns 0
, the presentation is a window.
To set the height and width of the slide show window in pixels, use the Height
property and the Width
property. To set its position, use the Top
property to specify the distance in pixels of the top edge of the presentation from the top of the window or screen and the Left
property to specify the distance in pixels of the left edge of the presentation from the left edge of the window or the screen.
Apart from controlling the position and size of the presentation, most of the actions you can take with a presentation involve the View
object. To find out which slide is displayed, return the CurrentShowPosition
property:
MsgBox ActivePresentation.SlideShowWindow.View.CurrentShowPosition
To display the first slide in the presentation, use the First
method. To display the last slide, use the Last
method:
ActivePresentation.SlideShowWindow.View.First ActivePresentation.SlideShowWindow.View.Last
To display the next slide, use the Next
method. To display the previous slide, use the Previous
method. Here's an example:
ActivePresentation.SlideShowWindow.View.Previous
To display a particular slide in the slide show, use the GotoSlide
method of the View
object, using the Index
argument to specify the slide number. For example, the following statement displays slide 5 in the first open slide show window:
Application.SlideShowWindows(1).View.GotoSlide Index:=5
To display a black screen, set the State
property of the View
object to ppSlideShowBlackScreen
. To display a white screen, set the State
property to ppSlideShowWhiteScreen
:
ActivePresentation.SlideShowWindow.View.State = ppSlideShowBlackScreen ActivePresentation.SlideShowWindow.View.State = ppSlideShowWhiteScreen
To toggle the black screen or white screen off and start the show running again, set the State
property to ppSlideShowRunning
.
To pause the presentation, set the State
property of the View
object to ppSlideShowPaused
. To start the show again, set the State
property to ppSlideShowRunning
, as in this example:
With ActivePresentation.SlideShowWindow.View .State = ppSlideShowPaused .State = ppSlideShowRunning End With
To start a custom show running, use the GotoNamedShow
method and use the SlideShowName
argument to specify the name of the custom show. For example, the following statement starts the custom show named New Show
running:
SlideShowWindows(1).GotoNamedShow SlideShowName:="New Show"
To exit a custom show, use the EndNamedShow
method and then use the Next
method to advance the presentation. PowerPoint then displays the first slide in the full presentation:
With ActivePresentation.SlideShowWindow.View .EndNamedShow .Next End With
PowerPoint VBA provides many ways to access and manipulate shapes.
Describe what the following line of code does:
ActivePresentation.Slides(2).Shapes(1).Delete
Using PowerPoint headers and footers can be a convenient way to provide continuity for presentations as well as identifying each element.
In this chapter, you worked with several examples showing how to manipulate footers for slides. Why were there no examples illustrating how to manipulate headers for slides?
To create a custom slide show, you use the Add
method of the NamedSlideShows
collection.
The syntax when using the Add
method of the NamedSlideShows
collection is
expression
.Add(Name, SafeArrayOfSlideIDs)
Explain what the four components of this line of code are and do.
3.147.71.94