Article ID: 149088
Article Last Modified on 1/19/2007
C:\Msoffice\Powerpnt\Pptexample.ppt
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
Unbound Object Frame:
Name: pptFrame
SizeMode: Zoom
Enabled: Yes
Locked: No
' Initialize variables.
Const FIRSTSLIDE = 256
Dim pptobj As Object
Dim slideindex As Long, slidecount As Long
Add the following error handling procedure:
Sub ErrHandler()
Dim strError As String
Dim errObj As Error
strError = " "
If DBEngine.Errors.Count > 0 Then
For Each errObj In DBEngine.Errors
strError = strError & Format$(errObj.Number)
strError = strError & " : " & errObj.Description
strError = strError & " (" & errObj.Source & ") . "
strError = strError & Chr$(13) & Chr$(10)
Next
MsgBox strError
Else
MsgBox "Error: " & Err & " " & Error
End If
End Sub
Private Sub Form_Load()
On Error GoTo Form_Load_Error
Dim holder As Long, present As Object
' Start Microsoft Powerpoint and open an existing presentation.
holder = Shell("c:\msoffice\powerpnt\powerpnt.exe")
Set pptobj = CreateObject("PowerPoint.Application")
Set present = pptobj.Presentations.Open _
("c:\msoffice\powerpnt\pptexample.ppt")
' Determine the number of slides in the presentation.
slidecount = present.Slides.Count - 1 + FIRSTSLIDE
' Close the presentation. Use only 1 of the following lines
'Office 97 Syntax
pptobj.Presentations _
("c:\msoffice\powerpnt\pptexample.ppt").Close
'Office 7.0 Syntax
present.Close
Set present = Nothing
Exit Sub
Form_Load_Error:
ErrHandler
Exit Sub
End Sub
Private Sub Form_Unload(Cancel As Integer)
On Error GoTo Form_Unload_Error
' Close Powerpoint.
pptobj.Quit
Exit Sub
Form_Unload_Error:
ErrHandler
Exit Sub
End Sub
Private Sub insertShow_Click()
On Error GoTo insertShow_Click_Error
' 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
'Use only 1 of the followin lines of code
.Class = "Microsoft Powerpoint Slide" 'Office 97 Syntax
.Class = "powerpoint.slide.7" 'Office 7.0 syntax
.OLETypeAllowed = acOLELinked
.SourceDoc = "c:\msoffice\powerpnt\pptexample.ppt"
.SourceItem = FIRSTSLIDE
.Action = acOLECreateLink
End With
' Set slide index to the first slide.
slideindex = FIRSTSLIDE
frstSlide.setfocus
insertShow.enabled=false
Exit sub
insertShow_Click_Error:
ErrHandler
Exit Sub
End Sub
Private Sub frstSlide_Click()
On Error GoTo frstSlide_Click_Error
' Set SourceItem property to first slide and create a link.
With pptFrame
.SourceItem = FIRSTSLIDE
.Action = acOLECreateLink
End With
slideindex = FIRSTSLIDE
Exit Sub
frstSlide_Click_Error:
ErrHandler
Exit Sub
End Sub
Private Sub nextSlide_Click()
On Error GoTo nextSlide_Click_Error
' Determine if current slide is last slide.
If slideindex< slidecount Then
slideindex = slideindex + 1
' Set SourceItem property to next slide and create a link.
With pptFrame
.SourceItem = slideindex
.Action = acOLECreateLink
End With
Else
MsgBox "This is the last slide."
End If
Exit Sub
nextSlide_Click_Error:
ErrHandler
Exit Sub
End Sub
Private Sub previousSlide_Click()
On Error GoTo previousSlide_Click_Error
' Determine if current slide is first slide.
If slideindex > FIRSTSLIDE Then
slideindex = slideindex - 1
' Set SourceItem property to previous slide and create a link.
With pptFrame
.SourceItem = slideindex
.Action = acOLECreateLink
End With
Else
MsgBox "This is the first slide."
End If
Exit Sub
previousSlide_Click_Error:
ErrHandler
Exit Sub
End Sub
Private Sub lastSlide_Click()
On Error GoTo lastSlide_Click_Error
' Set SourceItem property to previous slide and create a link.
With pptFrame
.SourceItem = slidecount
.Action = acOLECreateLink
End With
slideindex = slidecount
Exit Sub
lastSlide_Click_Error:
ErrHandler
Exit Sub
End Sub
133789 WT1229: Getting Started Controlling PowerPoint Through OLE
Keywords: kbhowto kbinterop kbprogramming KB149088