Search code examples
vbafilepowerpointlocked

PowerPoint 2013 macro keeps file locked open after close command


I have a PowerPoint VBA function that opens presentations, copies slides into the active presentation, then closes the source presentation. It worked fine in 2010, but fails in 2013 (all on Windows 7) if it tries to open the same presentation more than once. It appears to me that after the presentation.close command is issued, the window is closed, but the file remains locked open until the VBA code exits. So if the code attempts to open that file again it returns the error: "Method 'Open' of object 'Presentations' failed"

Here's a simplified form of the function I'm running that behaves the same way. I've had a colleague test this again in PowerPoint 2010 and it runs fine. I've also had a colleague test it under his 2013 to make sure it's not something with my particular installation.

Sub testopen()

Dim ppFile As Presentation
Dim i As Integer

Const fpath = "C:\test.pptx"

For i = 1 To 2
    Set ppFile = Application.Presentations.Open(fpath)
    ppFile.Close
    Set ppFile = Nothing
Next i

End Sub

The file test.pptx is just a blank presentation. In debug mode I can see the file opens and closes on the first loop, then on the second loop the open command fails and I can see in Windows explorer that the hidden temporary file still exists, indicating the file is still open, until I exit the VBA code. I also verified that the file is held open by adding in a function to check the file open status.

I've spent probably an hour googling this and cannot find any other descriptions of this problem. I'm sure I can implement a workaround but it's driving me crazy that I can't find any other reports of seemingly such a simple issue. Any suggestions are greatly appreciated! Thanks.


Solution

  • Setting the file as Read Only resolved the issue. The open command is now:

    Set ppFile = Application.Presentations.Open(fpath, msoTrue)
    

    Also, saving the file before closing it resolved the issue. For that, add:

    ppFile.Save
    

    Interestingly, I had already tried setting the Saved property to True (ppFile.Saved = msoTrue), which does NOT work. Thanks to Michael for his suggestion on the VBS script. That does work and I had never run an external VBS script so I learned something new. In this case, I'd prefer to stick with a VBA solution.