Tobi A.
Tobi A.

Reputation: 31

Unable to open PowerPoint File using VBA

I like to open a PowerPoint file from Excel. I have tried it several times but it does not work.

The Problem sounds similar to these:

not able to Open Powerpoint using VBA

The only difference is, I get another error code:

'Laufzeitfehler '-2147024894 (80070002)': Die Methode 'Open' für das Objekt 'Presentations' ist fehlgeschlagen.

I checked that the Microsoft PowerPoint 16.0 Object Library ist activated. And i checked the filepath several times.

Does anyone have an idea what the mistake can be?

Sub sub_powerpoint_test()
Dim ObjPPT As PowerPoint.Application
Dim ObjPresentation As PowerPoint.Presentation
Dim str_FileName_PPTX As String

Set ObjPPT = CreateObject("PowerPoint.Application")
ObjPPT.Visible = msoCTrue


'Get PPT Filename
If Len(Dir(ThisWorkbook.Path & "\*.pptx")) = 0 Then
   MsgBox "PPTX file does NOT exist in this folder."
Else
     str_FileName_PPTX = ThisWorkbook.Path & Dir(ThisWorkbook.Path & "\*.pptx")
     Debug.Print str_FileName_PPTX
 End If


Set ObjPresentation = ObjPPT.Presentations.Open(str_FileName_PPTX, Untitled:=msoTrue)

End Sub

The error occures in the Open line at the end.

Upvotes: 1

Views: 1334

Answers (1)

Tobi A.
Tobi A.

Reputation: 31

I found the solution. The Problem was a missing "\" in the path.

The corrected code is:

If Len(Dir(ThisWorkbook.Path & "\*.pptx")) = 0 Then
   MsgBox "PPTX file does NOT exist in this folder."
Else
    str_FileName_PPTX = ThisWorkbook.Path & "\" & Dir(ThisWorkbook.Path & "\*.pptx")
    Debug.Print str_FileName_PPTX
End If

Upvotes: 2

Related Questions