This article describes how to use the Microsoft Visual Basic forApplications Presentations.Open method. The Open method opens an existingMicrosoft PowerPoint presentation.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers 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 requirements. NOTE
: The following macro examples work only in PowerPoint. Visual Basic for Applications macros are not supported by the Microsoft PowerPoint Viewer. For additional information, click the following article number to view the article in the Microsoft Knowledge Base:
There are four parameters that you can use to control the behavior of theOpen method:
The FileName parameter is the path and name of the presentation that you want to open. The following Visual Basic example opens a file named test.ppt
located in the C:\ root directory.
Sub OpenPresentation() Presentations.Open "c:\test.ppt" End Sub
The ReadOnly parameter is used to open a presentation as Read-Only. Thefollowing example opens a file named test.ppt
Sub OpenPresentationReadOnly() Presentations.Open "c:\test.ppt", msoTrue End Sub
The parameter, msoTrue, tells PowerPoint to open the presentationReadOnly. When test.ppt
is open, the following text appears in the title bar:
If you do not specify the msoTrue parameter, it is set to msoFalse (thedefault), and the presentation opens as Read/Write.
The Untitled parameter is used to create a copy of the presentation. Thefollowing example opens a copy of test.ppt
Sub OpenCopy() Presentations.Open "c:\test.ppt", Untitled:=msoTrue End Sub
If you do not specify the msoTrue parameter, it is set to msoFalse (thedefault), and the file name only appears in the title bar:
The WithWindow parameter open a presentation without making itvisible. The following example opens test.ppt
, does not show it, and then closes it.
Sub OpenInvisible() Dim MyPres As Presentation Set MyPres = Presentations.Open("c:\test.ppt", _ WithWindow:=msoFalse) ' Close the presentation. MyPres.Close End Sub
: When you are finished with the invisible presentation, make sure you close the presentation.
If you do not specify the msoFalse parameter, it is set to msoTrue (thedefault).
This code example:
Presentations.Open "c:\test.ppt", WithWindow:=msoTrue
is equivalent to this code example:
Using the Return Value
The Open method returns a reference to a presentation object. Once you havethe object reference, you can use it to access methods and properties ofthe presentation.
Sub GetReturnValue() Dim MyPres As Presentation Set MyPres = Presentations.Open("c:\test.ppt") ' This counts the number of slides in test.ppt. MsgBox MyPres.Slides.Count End Sub
: To use the return value of a method, set the method to a variable and enclose the parameters in parentheses.
Error Trapping the Open Method
The following sample code demonstrates how you can trap errors that mayoccur when you use the Open method:
Sub ErrorTrapOpen() On Error Resume Next ' Clear all values in the Err object. Err.Clear Dim MyPres As Presentation Set MyPres = Presentations.Open("c:\file does not exist.ppt") ' If error occurred when opening the file, display the error message. If Err.Number <> 0 Then MsgBox Err.Description, vbCritical, "Error " & Err.Number End If End Sub
The On Error Resume Next statement allows the macro to continue to executestarting with the statement that follows the statement thatgenerated the error. If you do not use the On Error Resume Next statement,you will receive a run-time error if the file cannot be opened and themacro quits. When a run-time error occurs, information about that error isstored in the Err object.
For more information about the Err object
, ask the Microsoft PowerPoint Office Assistant.