Search code examples
excelvbapowerpoint

Closing one single PPT Presentation via VBA (Excel)


I would like to close a PPT via VBA in Excel. The problem is with the Code "PP.Close". (last line of code)

  1. It also closes other open PPT Presentations not only the one I "target"
  2. Sometimes it closes the Presentation AND the application, sometimes just the Presentation, which in my opinion is strange.

Does somebody know how I can solve those issues?

Kind regards and thank you in advance

Sub Saveas_PPT()
Dim PP As PowerPoint.Presentation
Dim Sl As PowerPoint.Slide
Dim sh As Variant
Dim company As String
'Dim ws_company As Worksheet
Set Dropdown.ws_company = Tabelle2
company = Dropdown.ws_company.Range("C2").Value

Dim strPOTX As String
Dim strPfad As String
Dim pptApp As Object

strPfad = "C:\Users\Michael\" 'Ordner der Vorlage
 strPOTX = "Test.pptx" 'Hier theoretisch auch 
 filepicker möglich

 Set pptApp = New PowerPoint.Application

 Dim pptVorlage As String
 pptVorlage = strPfad & strPOTX


 Set PP = pptApp.Presentations.Open(pptVorlage)



 pptApp.ActivePresentation.UpdateLinks 


'  Dim x As Integer
'  For Each prs In PP.Application.Presentations
'
'  x = x + 1
'  

'If x > 1 Then
''pptApp.ActivePresentation.Close ' Hier auch
'  'PP.Close 'Problem: Hier wird alles geschlossen
'  Exit Sub
'End If
'  Next prs

 'pp.Application.Quit 'vorher FUnktion die schaut ob mehr als eine PPT offen sind. Wenn nur eine dann quit, sonst nur diese hier schließen
 pp.Close
 End Sub

Solution

  • My suggestion is to use the AppActivate function.

    It requires the title of the window.
    I'm not sure it will work but at least the correct presentation will be active.

    AppActivate("the PowerPoint title")
    

    Replace the PowerPoint title with what the Windows title is.

    You could, if that too fails, perhaps use sendkeys after it to ALT + F4 to close the active window, which should be the presentation you want.