【发布时间】:2021-12-01 09:29:16
【问题描述】:
我在这里遇到了一些麻烦。我的代码停止运行时错误 -2147467259 (80004005) Mehod 'Open' of object 'Presentations: failed.
此代码显示警告,提示输入源文件夹和目标文件夹,并循环访问源文件夹中的所有文件,打开每个文件并将每张幻灯片导出为单独的文件,并再次导出到文件夹中的最后一个文件。
我放了几个 msgbox 看看是不是名字有问题,根据 MVP Andy Pope 的一些代码重新编写了打开的文件段,但什么都没有。
非常感谢任何帮助。
Sub ExportIndividualSlides()
''Application.DisplayAlerts = False
Dim ObjPPAPP As New PowerPoint.Application
Dim objPPPres As PowerPoint.Presentation
Dim objPPSlide As PowerPoint.Slide
'Initial directory path.
Dim SourceFolder As String
Dim TargetFolder As String
SourceFolder = "c:\source"
TargetFolder = "c:\target"
Dim Slide As Long
Dim SourcePresentation As Presentation
Dim SourcePresentationName As String
Dim TargetFileName As String
Dim SourceNamePath
Debug.Print "-- Start --------------------------------"
ActiveWindow.ViewType = ppViewNormal
'Loop through ppt* files only in source folder
SourcePresentationName = Dir(SourceFolder & "\*.ppt*")
MsgBox "SPN:" & SourcePresentationName
While (SourcePresentationName <> "")
SourceNamePath = SourceFolder & "\" & SourcePresentationName
Debug.Print " SourceNamePath"
MsgBox SourceNamePath
Set ObjPPAPP = New PowerPoint.Application
ObjPPAPP.Visible = True
Set objPPPres = ObjPPAPP.Presentations.Open(SourceNamePath)
' On Error GoTo errorhandler
' Open source files
Set SourcePresentation = Presentations.Open(FileName:=SourcePresentationName, WithWindow:=False)
Debug.Print " SourcePresentation: " & SourcePresentation.Name
' Loop through slides
For Slide = 1 To SourcePresentation.Slides.Count
Debug.Print " Slide: " & Slide
' Create a unique filename and save a copy of each slide
TargetFileName = Left(SourcePresentation.Name, InStrRev(SourcePresentation.Name, ".") - 1) & " [" & Slide & "].pptx"
TargetNamePath = TargetFolder & "\" & TargetFileName
Debug.Print " TargetNamePath: " & TargetNamePath
SourcePresentation.Slides(Slide).Export TargetNamePath, "PPTX"
Next Slide
objPPPres = Nothing
SourcePresentation.Close
SourcePresentationName = Dir
Wend
On Error GoTo 0
Exit Sub
errorhandler:
Debug.Print Err, Err.Description
Resume Next
End Sub
【问题讨论】:
-
您打开每个文件两次,一次是在新实例中(使用
SourceNamePath),另一次是在当前实例中(使用SourcePresentationName)?你的意思是这样做吗?为什么要在这里新建一个 PPT 实例? -
蒂姆,我不是故意的。我试图变化。无意留下第二个没有评论。两者都给了我同样的错误。提前致谢。
标签: vba powerpoint