Convidado 15/8/2011, 13:26
Using Automation to Display a PowerPoint Presentation in a Form
This sample shows you how to display slides from PowerPoint on a form in Access. This technique uses Automation in Access to open a PowerPoint presentation and to link to the first slide. Viewing other slides is accomplished by changing the SourceItem property, which enables you to link to different slides.
Note To use this technique, you must have both PowerPoint and Access installed on your computer. You also need to create a PowerPoint presentation (.ppt). Throughout the procedure, replace the following file name with the name and path of your file: C:\Program Files\Microsoft Office\Office\Pptexample.ppt The sample provided as a download contains a sample presentation.
The following example creates a form with an unbound object frame control and five command buttons for linking to a PowerPoint presentation and for moving through its slides.
To display Microsoft PowerPoint slides on a form, follow these steps:
1.In a new Access database, create a form in Design view.
2.Add the following five controls to the form:
Command button
◦Name: insertShow
◦Caption: Get Presentation
◦Enabled: Yes
Command button
◦Name: frstSlide
◦Caption: First Slide
◦Enabled: No
Command button
◦Name: nextSlide
◦Caption: Next Slide
◦Enabled: No
Command button
◦Name: previousSlide
◦Caption: Previous Slide
◦Enabled: No
Command button
◦Name: lastSlide
◦Caption: Last Slide
◦Enabled: No
3.Add an unbound object frame control to the form. In the Insert Object box, click Create New button, select Bitmap Image as the Object Type, and then click OK. Note that the object frame appears as a blank space on the form.
4.Display the property sheet for the unbound object frame, and then set its properties as follows:
Unbound Object Frame
◦Name: pptFrame
◦SizeMode: Zoom
◦Enabled: Yes
◦Locked: No
5.On the View menu, click Code to open the form module.
6.Add the following code to the General Declarations section:
Option Explicit
' Initialize variables.
Private mcolSlideIDs As Collection
Private mlngSlideIndex As Long
7.In the Object list, click insertShow. In the Procedure list, click Click, and then add the following code:
Private Sub insertShow_Click()
On Error GoTo insertShow_Click_Error
' Open PowerPoint
Dim strPowerPointFile As String
Dim pptobj As PowerPoint.Application
Set pptobj = New PowerPoint.Application
pptobj.Visible = True
pptobj.WindowState = ppWindowMinimized
strPowerPointFile = CurrentProject.Path & "\Access2PowerPoint.ppt"
' Fill a collection with all Slide IDs.
With pptobj.Presentations.Open(strPowerPointFile)
Set mcolSlideIDs = New Collection
Dim ppSlide As PowerPoint.Slide
For Each ppSlide In .Slides
mcolSlideIDs.Add ppSlide.SlideID
Next
.Close
End With
' Close PowerPoint
pptobj.Quit
Set pptobj = Nothing
' Make object frame visible and enable "navigation" buttons.
pptFrame.Visible = True
frstSlide.Enabled = True
lastSlide.Enabled = True
nextSlide.Enabled = True
previousSlide.Enabled = True
' Specify OLE Class, Type, SourceDoc, SourceItem and other properties.
With pptFrame
.Class = "Microsoft Powerpoint Slide"
.OLETypeAllowed = acOLELinked
.SourceDoc = strPowerPointFile
End With
SetSlide 1
frstSlide.SetFocus
insertShow.Enabled = False
Exit Sub
insertShow_Click_Error:
MsgBox Err.Number & " " & Err.Description
Exit Sub
End Sub
8.In the Object list, click frstSlide. In the Procedure list, click Click, and then add the following code:
Private Sub frstSlide_Click()
SetSlide 1
End Sub
9.In the Object list, click lastSlide. In the Procedure list, click Click, and then add the following code:
Private Sub lastSlide_Click()
SetSlide mcolSlideIDs.Count
End Sub
10.In the Object list, click nextSlide. In the Procedure list, click Click, and then add the following code:
Private Sub nextSlide_Click()
SetSlide mlngSlideIndex + 1
End Sub
11.In the Object list, click previousSlide. In the Procedure list, click Click, and then add the following code:
Private Sub previousSlide_Click()
SetSlide mlngSlideIndex - 1
End Sub
12.Add the following procedure:
Private Sub SetSlide(ByVal ID As Integer)
On Error GoTo ErrorHandler
Select Case ID
Case Is > mcolSlideIDs.Count
MsgBox "This is the last slide."
Case 0
MsgBox "This is the first slide."
Case Else
mlngSlideIndex = ID
With pptFrame
.SourceItem = mcolSlideIDs(mlngSlideIndex)
.Action = acOLECreateLink
End With
End Select
Exit Sub
ErrorHandler:
MsgBox Err.Number & " " & Err.Description
Exit Sub
End Sub
13.Close and save the form module.
14.Switch the form to Form view, and then click Get Presentation. Next, click the other buttons to move through the presentation.