奇怪的VBA Presentation.close运行时错误#91

时间:2019-03-29 07:00:39

标签: vba runtime powerpoint

打开Powerpoint并在第一次运行此代码,一切正常 但在第二次运行时,系统抛出了runtime error 91并突出显示了oPres.Close

我完全关闭了powerpoint,然后再次运行它,第一轮可以,但是第二轮发生错误。

我有点困惑。

  Sub PrintAll()

Dim CurrentFolder As String
Dim FileName As String
Dim myPath As String
Dim myPath2 As String
Dim UniqueName As Boolean
Dim PDFName As String

Dim strCurrentFile As String
Dim strFileSpec As String

Dim sldTemp As Slide
Dim lngTemp As Long
Dim lngCount As Long

Dim PP As Object
Dim oPres As Object


    CurrentFolder = ActivePresentation.Path & "\" 'get current folder
    strFileSpec = "*.ppt"
    strCurrentFile = Dir$(CurrentFolder & strFileSpec)  

FileName = Mid(strCurrentFile, InStrRev(strCurrentFile, "\") + 1, _
   InStrRev(strCurrentFile, ".") - InStrRev(strCurrentFile, "\") - 1)  
     Debug.Print "FileName: " + FileName

     PDFName = CurrentFolder & FileName & ".pdf"    
     Debug.Print "PDFName: " + PDFName
     Set PP = CreateObject("Powerpoint.Application") ' Late binding  

'-----------------------------------------Start Loop-----------------------------------------

While strCurrentFile <> ""

    On Error Resume Next

    Set oPres = PP.Presentations.Open(CurrentFolder & strCurrentFile)  

          Debug.Print "CurrentFolder: " + strCurrentFile

          If Err.Number <> 0 Then
            Debug.Print "Unable to open " & FileName
          End If

    FileName = Mid(strCurrentFile, InStrRev(strCurrentFile, "\") + 1, _
     InStrRev(strCurrentFile, ".") - InStrRev(strCurrentFile, "\") - 1)   
    Debug.Print "FileName: " + FileName

    PDFName = CurrentFolder & FileName & ".pdf"    

    Debug.Print "PDFName: " + PDFName

    On Error GoTo 0


oPres.Close   'The 91 error occurred  here 


  strCurrentFile = Dir()  
Wend

'-----------------------------------------End Loop-----------------------------------------

PP.Quit  
Set PP = Nothing

End Sub

1 个答案:

答案 0 :(得分:0)

谢谢Siddharth。 我想我的根本原因是:Powerpoint保护方法的新版本将为每个未保存的PPT文件生成一个临时副本。并在下次打开相同的PPT文件时弹出一个对话框。

解决方案:在关闭前添加“ oPres.Save”。