PowerPoint s Presentation Object

     

PowerPoint's Presentation Object

In PowerPoint, the Presentation object represents a presentation file (.PPT) that is open in the PowerPoint application window. You can use VBA to create new presentations, open or delete existing presentations, save and close presentations, and more. The next section takes you through various techniques for specifying presentations in your VBA code; then we'll look at some Presentation object properties and methods .

Specifying a Presentation Object

If you need to do something with a presentation, or if you need to work with an object contained in a specific presentation (such as a slide), you need to tell PowerPoint which presentation you want to use. VBA gives you three ways to do this:

Use the Presentations object ” The Presentations object is the collection of all open presentation files. To specify a particular presentation, either use its index number (where 1 represents the first presentation opened) or enclose the presentation filename in quotation marks. For example, if Proposal.ppt were the first presentation opened, the following two statements would be equivalent:

 Presentations(1) Presentations("Proposal.ppt") 

Use the ActivePresentation object ” The ActivePresentation object represents the presentation that currently has the focus.

Use the Presentation property ” Open slide show windows have a Presentation property that returns the name of the underlying presentation. For example, the following statement uses the currPres variable to store the name of the presentation in the first slide show window:

 currPres = SlideShowWindows(1).Presentation 

Opening a Presentation

To open a presentation file, use the Open method of the Presentations collection. The Open method has several arguments you can use to fine-tune your presentation openings, but only one of these is mandatory. Here's the simplified syntax showing the one required argument (for the rest of the arguments, look up the Open method in the VBA Help system):

 Presentations.Open(  FileName  ) 

FileName

The full name of the presentation file, including the drive and folder that contain the file.

For example, to open a presentation named Proposal.ppt in the C:\My Documents folder, you would use the following statement:

 Presentations.Open "C:\My Documents\Proposal.ppt" 

Creating a New Presentation

If you need to create a new presentation, use the Presentations collection's Add method:

 Presentations.Add(  WithWindow  ) 

WithWindow is a Boolean value that determines whether or not the presentation is created in a visible window. Use True for a visible window (this is the default); use False to hide the window.

Presentation Object Properties

Here's a list of a few common properties associated with Presentation objects:

Presentation .FullName ” Returns the full pathname of the specified Presentation . The full pathname includes the presentation's path (the drive and folder in which the file resides) and the filename.

Presentation .Name ” Returns the filename of the Presentation .

Presentation .Path ” Returns the path of the Presentation file.

NO PATH?

A new, unsaved presentation's Path property returns an empty string ("").


Presentation .Saved ” Determines whether changes have been made to the specified Presentation since it was last saved.

Presentation .SlideMaster ” Returns a Master object that represents the slide master for the specified Presentation .

Presentation .Slides ” Returns a Slides object that represents the collection of Slide objects contained in the specified Presentation .

Presentation .SlideShowSettings ” Returns a SlideShowSettings object that represents the slide show setup options for the specified Presentation .

Presentation .TitleMaster ” Returns a Master object that represents the title master for the specified Presentation .

Presentation Object Methods

A Presentation object has methods that let you save the presentation, close it, print it, and more. Here are the methods you'll use most often:

Presentation .ApplyTemplate ” Applies a design template to the specified Presentation . This method uses the following syntax:

  Presentation  .ApplyTemplate(  FileName  ) 

Presentation

The Presentation object to which you want to apply the template.

FileName

The full name of the template ( .POT ) file.

For example, the following statement applies the Dads Tie template to the active presentation:

 ActivePresentation.ApplyTemplate _     "C:\Microsoft Office\Templates\Presentation Designs\Dads Tie.pot" 

Presentation .Close ” Closes the specified Presentation . If the file has unsaved changes, PowerPoint will ask the user if he or she wants to save those changes.

Presentation .NewWindow ” Opens a new window for the specified Presentation .

Presentation .PrintOut ” Prints the specified Presentation using the following syntax:

  Presentation  .PrintOut(  From, To, PrintToFile, Copies, Collate  ) 

Presentation

The Presentation object you want to print.

From

The page number from which to start printing.

To

The page number of the last page to print.

PrintToFile

The name of a file to which you want the presentation printed.

Copies

The number of copies to print. The default value is 1.

Collate

If this argument is True and Copies is greater than 1, VBA collates the copies.

Presentation .Save ” Saves the specified Presentation . If the presentation is new, use the SaveAs method instead.

Presentation .SaveAs ” Saves the specified Presentation to a different file. Here's the syntax for the SaveAs method:

  Presentation  .SaveAs(  FileName, FileFormat, EmbedTrueTypeFonts  ) 

Presentation

The Presentation object you want to save to a different file.

FileName

The full name of the new presentation file, including the drive and folder where you want the file to reside.

FileFormat

The PowerPoint format to use for the new file. Use one of the predefined ppSaveAs constants (such as ppSaveAsPresentation or ppSaveAsHTML ).

EmbedTrueTypeFonts

If True, PowerPoint embeds the presentation's TrueType fonts in the new file.

The Juggling Application

Throughout this chapter, I'll put the PowerPoint objects, methods, and properties that we talk about to good use in an application that builds an entire presentation from scratch. This presentation will consist of a series of slides that provide instructions on how to juggle.

The code for the application consists of six procedures:

Main ” This procedure ties the entire application together by calling each of the other procedures in the module.

CreateJugglingPresentation ” This procedure creates a new Presentation object and saves it.

AddJugglingSlides ” This procedure adds the slides to the presentation and then formats them.

SetUpStartPage ” This procedure adds and formats text for the presentation title page.

SetUpJugglingPages ” This procedure adds and formats a title, picture, and instruction text for each of the four pages that explain how to juggle.

RunJugglingSlideShow ” This procedure asks the user if he or she wants to run the slide show and then runs it if Yes is chosen .

To get started, Listing 9.1 shows the Main procedure.

Listing 9.1. This Procedure Ties Everything Together by Calling Each of the Code Listings Individually
 ' Global variable Dim pres As Presentation Sub Main()     '     ' Create the presentation file     '     CreateJugglingPresentation     '     ' Add the slides     '     AddJugglingSlides     '     ' Set up the title page     '     SetUpStartPage     '     ' Set up the Juggling pages     '     SetUpJugglingPages     '     ' Save it and then run it     '     pres.Save     RunJugglingSlideShow End Sub 

First, the pres variable is declared as a Presentation object. Notice that this variable is defined at the top of the module, before any of the procedures. When you define a variable like this, it means that it can be used in all the procedures in the module. Then Main begins by calling the CreateJugglingPresentation procedure, shown in Listing 9.2. From there, the other procedures (discussed later in this chapter) are called, and the presentation is saved.

Listing 9.2. This Procedure Creates a New Presentation and then Saves It
 Sub CreateJugglingPresentation()     Dim p As Presentation     '     ' If the old one is still open, close it without saving     '     For Each p In Presentations         If p.Name = "Juggling" Then             p.Saved = True             p.Close         End If     Next p     '     ' Create a new Presentation object and store it in pres     '     Set pres = Presentations.Add     pres.SaveAs FileName:="Juggling.ppt" End Sub 

A For Each...Next loop runs through each open presentation and checks the Name property. If it equals Juggling.ppt , we know the file is already open. If it's open (say, from running the application previously), the procedure closes it without saving it. The pres variable is Set and then the presentation is saved using the SaveAs method.

graphics/note_icon.gif

The presentation and code used in this chapter's sample application can be found on my Web site at the following address:

http://www.mcfedries.com/ABGVBA/Chapter09.ppt




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

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