【问题标题】:Open method not working to open ppts from a ppt打开方法无法从 ppt 打开 ppts
【发布时间】: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


【解决方案1】:

这对我有用:

Sub ExportIndividualSlides()
    'use const for fixed values
    Const SOURCE_FOLDER As String = "c:\source\" 'include terminal \
    Const TARGET_FOLDER As String = "c:\target\"
    
    Dim objPres As PowerPoint.Presentation
    Dim Slide As Long
    Dim SourcePresentationName As String
    Dim TargetFileName As String
    Dim TargetNamePath As String
    Dim SourceNamePath
    
    Debug.Print "-- Start --------------------------------"
    ActiveWindow.ViewType = ppViewNormal
    
    On Error GoTo errorhandler
    
    'Loop through ppt* files only in source folder
    SourcePresentationName = Dir(SOURCE_FOLDER & "*.ppt*")
    Do While Len(SourcePresentationName) > 0
        
        SourceNamePath = SOURCE_FOLDER & SourcePresentationName
        Debug.Print "Opening: " & SourceNamePath
        
        Set objPres = Presentations.Open(SourceNamePath)
        
        ' Loop through slides
        For Slide = 1 To objPres.Slides.Count
            
            Debug.Print "   Slide: " & Slide
            ' Create a unique filename and save a copy of each slide
            TargetFileName = Left(objPres.Name, InStrRev(objPres.Name, ".") - 1) & " [" & Slide & "].pptx"
            TargetNamePath = TARGET_FOLDER & TargetFileName
            Debug.Print "   TargetNamePath: " & TargetNamePath
            objPres.Slides(Slide).Export TargetNamePath, "PPTX"
        
        Next Slide
        
        objPres.Close
        
        SourcePresentationName = Dir() 'next file
    Loop
    
    Exit Sub
     
errorhandler:
    Debug.Print Err, Err.Description
    Resume Next
    
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-26
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-29
    • 2019-11-30
    • 1970-01-01
    相关资源
    最近更新 更多