Link to home
Start Free TrialLog in
Avatar of Jamie Garroch (MVP)
Jamie Garroch (MVP)Flag for United Kingdom of Great Britain and Northern Ireland

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:

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

Open in new window


The output is this:

Open presentations before : 1
Open presentations after : 2
Trying to close presentation 2
Open Presentations : 2

Open in new window


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

Open in new window


Why won't the presentation close at run time?
Avatar of Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj)
Flag of India image

If I am not wrong Slides.InsertFromFile will add a Slice in the active presentation instead of opening a new presentation.
Try replacing the line#8 with the following line...
Presentations.Open (sFile)

Open in new window

Avatar of Jamie Garroch (MVP)

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
Avatar of Jamie Garroch (MVP)
Jamie Garroch (MVP)
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial