Jamie Garroch (MVP)
asked on
PowerPoint Slides.InsertFromFile method adds a Presentation to the Presentations collection that can't be closed
I'm using the Slides.InsertFromFile method to inserts slides from a file into the active presentation and it causes a new Presentation object to be added to the Presentations collection (without a window) which then needs to be closed. The problem is that the Close method is not working at run time but it does in the Immediate window. Here's a test macro:
The output is this:
So the Close method failed to close the second presentation (and doesn't raise any error) but the following works when run in the Immediate window after running the above macro:
Why won't the presentation close at run time?
Sub TestInsertFromFile()
Dim lPres As Long
Const sFile = "C:\Temp\myPresentation.pptx"
lPres = Presentations.count
Debug.Print "Open presentations before : " & lPres
ActivePresentation.Slides.InsertFromFile sFile, 1, 2, 2
Debug.Print "Open presentations after : " & Presentations.count
' If there's more open presentations than before, close the last one in the collection
If Presentations.count > lPres Then
Debug.Print "Trying to close presentation " & Presentations.count
Presentations(Presentations.count).Close
Debug.Print "Open Presentations : " & Presentations.count
End If
End Sub
The output is this:
Open presentations before : 1
Open presentations after : 2
Trying to close presentation 2
Open Presentations : 2
So the Close method failed to close the second presentation (and doesn't raise any error) but the following works when run in the Immediate window after running the above macro:
Presentations(2).Close
Why won't the presentation close at run time?
ASKER
The Slides.InsertFromFile method inserts one or more files from the specified presentation into the active presentation and that's what I want to do. The issue is that it's causing the presentation to be opened in windowless mode and it won't allow me to close it after using this method. Looks like a Microsoft bug to me!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Try replacing the line#8 with the following line...
Open in new window