PPT2000: Introduction to Macro Programming in PowerPoint 2000ID: q222771
|
This article explains the steps to create a simple Microsoft
Visual Basic for Applications macro within Microsoft PowerPoint. The macro
you create will add a slide to your presentation, set a background texture
for a slide, set slide timings, and run a slide show.
This article is designed to introduce you to some of the tools and concepts
you need to become a macro programmer.
Microsoft provides programming examples for illustration only, without
warranty either expressed or implied, including, but not limited to, the
implied warranties of merchantability and/or fitness for a particular
purpose. This article assumes that you are familiar with the programming
language being demonstrated and the tools used to create and debug
procedures. Microsoft Support professionals can help explain the functionality
of a particular procedure, but they will not modify these examples to
provide added functionality or construct procedures to meet your specific
needs. If you have limited programming experience, you may want to contact
the Microsoft fee-based consulting line at (800) 936-5200. For more
information about the support options available from Microsoft, please see
the following page on the World Wide Web:
http://www.microsoft.com/support/supportnet/overview/overview.aspNOTE: The following macro examples only work from within the PowerPoint application. Visual Basic for Applications macros are not supported by the Microsoft PowerPoint Viewer. For additional information, please see the following article in the Microsoft Knowledge Base:
Q230746 PPT: Viewer: Presentation Macros Don't Run
<macro name> is not a valid name for a macroIf you see this message, just keep trying until you find a name that PowerPoint will accept.
Dim MySlide As Slide
When you type in the code, you probably noticed some interesting
things happen. After you press SPACE following the word "as", a list of the available data types appeared on your screen. This is
just one of the ways the Visual Basic Editor makes programming a little
easier.
Dim |
Indicates to the Visual Basic Editor you are about to declare a variable. There are several other methods available to declare variables, but this article discusses only the Dim method. |
MySlide |
Is the name you provide the variable. It is a good idea to give your variables meaningful descriptive names. X is an example of a poor variable name. Meaningful names make your code easier to read. |
As Slide |
Specifies the type of data the variable will contain. Inthis case, MySlide will have the data type Slide. |
Set MySlide = ActivePresentation.Slides.Add(1, ppLayoutTitle)
This code adds a new slide to the active presentation. The slide
that is created uses the Title Only auto layout.
Set MySlide |
Assigns an object reference to a variable or property. Using Set makes it easier to refer to that same object later in your code. |
ActivePresentation |
Tells the Visual Basic Editor you are referring to the presentation that is open in the active window. |
Add(1, ppLayoutTitle) |
Creates a new slide and adds it to the collection of slides in the presentation. The Add method takes two parameters: |
For more information about creating slides programmatically, click Microsoft PowerPoint Help on the Help menu, type "Add Slides" in the Office Assistant or the Answer Wizard, and then click Search to view the topic.TIP: When you type code, if the property and method list appears, you can select the item you want and then press TAB, which adds the object to your command and leave the cursor on the same line. Your macro code should now look similar to the following:
For more information about the slide AutoLayouts available, click Microsoft PowerPoint Help on the Help menu, type "PpPlaceHolder Type" in the Office Assistant or the Answer Wizard, and then click Search to view the topic.
Sub YourMacro ()
'
' Macro created 1/7/97 by You
'
Dim MySlide As Slide
Set MySlide = ActivePresentation.Slides.Add(1, ppLayoutTitle)
End Sub
NOTE: The text following ' apostrophe (on the same line) is a comment. Comments are ignored by the Visual Basic Editor. They are added to the code to make it easier to understand.
ActiveWindow.ViewType = ppViewSlideSorter
This changes the presentation to slide sorter view. Add this
so you can select the entire slide.
For more information about PowerPoint views, click Microsoft PowerPoint Help on the Help menu, type "views" in the Office Assistant or the Answer Wizard, and then click Search to view the topic.
MySlide.Select
This code selects the slide you created. MySlide is the object
reference you created with the Dim statement.
With ActiveWindow.Selection.SlideRange
.FollowMasterBackground = msoFalse
.Background.Fill.PresetTextured msoTextureRecycledPaper
End With
These commands tell PowerPoint that this particular slide does not
follow the master, and then set the background preset texture to the
recycled paper.
The With statement allows you to group commands that have common
references. If you use With to group multiple commands, you can improve the
performance of the macro, as well as save yourself a lot of typing.
ActiveWindow.Selection.SlideRange.FollowMasterBackground = msoFalse
ActiveWindow.Selection.SlideRange.Background.Fill.PresetTextured _
msoTextureRecycledPaper
The underscore in the second line is a continuation character. It tells
the Visual Basic Editor that you could not fit the specific command on
one line and are continuing the instruction on the next line.
You can see the advantage of using With statements; less typing and
faster code. The only downside to the With statement is it sometimes
makes the code more difficult to read, especially, if you nest a With
within another With statement.
MySlide.Shapes.Title.TextFrame.TextRange.Text = "Look What I Did!"
This command adds the text "Look What I Did!" into the title box of the
slide you created.
With ActivePresentation.Slides.Range.SlideShowTransition
.AdvanceTime = 5
.EntryEffect = ppEffectCheckerboardAcross
End With
AdvanceTime |
Specifies how long (in seconds) a particular slide is visible when running a slide show. |
EntryEffect |
Specifies the slide transition effect that runs just prior to the slide appearing. |
ActivePresentation.SlideShowSettings.Run
This code starts the slide show.
Sub YourMacro()
'
' Macro created <Date> by <You>
'
Dim MySlide As Slide
' Add a new slide to the presentation.
Set MySlide = ActivePresentation.Slides.Add(1, ppLayoutTitle)
' Change the presentation to slide sorter view.
ActiveWindow.ViewType = ppViewSlideSorter
' Select your slide.
MySlide.Select
' Apply a preset texture to the slide.
With ActiveWindow.Selection.SlideRange
.FollowMasterBackground = msoFalse
.Background.Fill.PresetTextured msoTextureRecycledPaper
End With
' Add text into title of the slide.
MySlide.Shapes.Title.TextFrame.TextRange.Text = "Look What I Did!"
' Set the slide timing and transition effect.
With ActivePresentation.Slides.Range.SlideShowTransition
.AdvanceTime = 5
.EntryEffect = ppEffectCheckerboardAcross
End With
' Start the slide show.
ActivePresentation.SlideShowSettings.Run
End Sub
For more information about using the sample code in this article, please
see the following article in the Microsoft Knowledge Base:
Q212536 OFF2000: How to Run Sample Code from Knowledge Base Articles
Additional query words: 9.00 ppt9 vba vbe ppt2k powerpt vba2k ppt9.0 ppt2000 program programming
Keywords : kbcode kbmacro kbprg kbdta kbdtacode kbpptvba
Version : WINDOWS:2000
Platform : WINDOWS
Issue type :
Last Reviewed: July 14, 1999