Access Cookbook - Ken Getz [272]
Discussion
Creating the presentation boils down to four basic steps:
Start PowerPoint (and shut it down once you're finished).
Create the presentation.
Loop through tblSlides, creating the slides one at a time.
For each slide, loop through the appropriate rows of tblParagraphs, placing and formatting text.
You'll find all the necessary code in basPowerPoint in 12-07.MDB. The following sections describe in detail how these steps work.
Starting and stopping PowerPoint
To create the presentation, you must first retrieve a reference to the PowerPoint Application object. If PowerPoint is already running, the GetObject function will be able to retrieve the object reference. If not, the code will jump to an error handler, which will try the CreateObject method. Once the procedure has created and saved the slide presentation, if the code started PowerPoint, it will try to close PowerPoint; if not, it will leave the application running. The following skeleton version of the CreatePresentation function (shown later in its entirety) handles the application startup and shutdown:
Public Function CreatePresentation(blnShowIt As Boolean, _
ByVal varTemplate As Variant, varFileName As Variant)
Dim app As PowerPoint.Application
Dim blnAlreadyRunning As Boolean
On Error GoTo HandleErrors
' Assume that PowerPoint was already running.
blnAlreadyRunning = True
Set app = GetObject(, "PowerPoint.Application")
' Do the work, creating the presentation.
If Not blnAlreadyRunning Then
app.Quit
End If
Set app = Nothing
ExitHere:
Exit Function
HandleErrors:
Select Case Err.Number
Case conErrCantStart
Set app = New PowerPoint.Application
blnAlreadyRunning = False
Resume Next
' Handle other errors...
End Select
Resume ExitHere
End Function
Creating the presentation
To create the presentation, you must add a new presentation to the application's collection of open presentations. To add a new item to the collection, use the Add method of the Presentations collection of the Application object:
' Get a reference to that new presentation.
Set pptPresentation = app.Presentations.Add(WithWindow:=False)
TIP
The Add method of the Presentations collection allows you to create the new presentation with or without a window. If you want PowerPoint to be visible while it's creating the presentation, you can set this parameter to True instead of False. However, if it's set to True, the code that creates the slides runs noticeably slower, and you'll have to contend with other user-interface issues (PowerPoint will request confirmation on overwriting existing presentations when you save this one, for example). We suggest leaving this parameter set to False unless you have some overriding reason to change it.
Once you've created the presentation, the code uses the ApplyTemplate method of the new Presentation object, given the name of the template you've chosen from frmPowerPoint:
If Len(varTemplate & "") > 0 Then
pptPresentation.ApplyTemplate varTemplate
End If
The code then calls the user-defined CreateSlides function, passing to it the new Presentation object, to create all the slides for the presentation.
This section and the previous one draw their code from the CreatePresentation function in basPowerPoint. Here's the function in its entirety:
Public Function CreatePresentation(blnShowIt As Boolean, _
ByVal varTemplate As Variant, varFileName As Variant)
' Highest-level routine. Actually create the
' presentation, and set up the slides.
Dim pptPresentation As PowerPoint.Presentation
Dim lngResult As Long
Dim app As PowerPoint.Application
Dim blnAlreadyRunning As Boolean
On Error GoTo HandleErrors
' Assume that PowerPoint was already running.
blnAlreadyRunning = True
Set app = GetObject(, "PowerPoint.Application")
' If the caller wants to see this happening, make the
' application window visible and set the focus there.
If blnShowIt Then
app.Visible = True
AppActivate "Microsoft PowerPoint"
End If
'