打开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
谢谢Siddharth。我认为我找到了根本原因:新版本的powerpoint保护方法将为每个PPT文件生成临时副本而不保存。并在下次打开相同的PPT文件时弹出一个对话框。
解决方案:关闭前添加“oPres.Save”。