2017-11-18 160 views
0

我喜歡從Excel打開PowerPoint文件。 我試了幾次,但它不起作用。無法使用VBA打開PowerPoint文件

的問題聽起來類似於這些:

not able to Open Powerpoint using VBA

唯一的區別是,我得到另一個錯誤代碼:

'Laufzeitfehler '-2147024894(80070002)': 死了Methode' 打開'fürdas Objekt'Presentations'ist fehlgeschlagen。

我檢查到Microsoft PowerPoint 16.0對象庫沒有激活。我檢查了幾次文件路徑。

有沒有人有一個想法是什麼錯誤?

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 

該錯誤發生在最後的打開行中。

回答

0

我找到了解決方案。問題是路徑中缺少「\」。

校正的代碼是:

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