在 Excel VBA 中保存 PowerPoint 文件时无法抑制的错误
Insuppressible Error when saving PowerPoint file in Excel VBA
在这被标记为重复之前: 它不是,因为所有其他保存错误似乎都得到了不同的 MsgBox
。
我正在编写一个从 Excel 打开和关闭 PowerPoint 演示文稿的宏。现在我遇到的问题是,当我尝试保存 PowerPoint 文件时,我会弹出一个消息框:
它说:"PowerPoint-Error while saving the file."
我的代码:
Dim pptPres As PowerPoint.Presentation
Dim pptApp As PowerPoint.Application
Set pptApp = New PowerPoint.Application
strPath = "S:\Folderxy\"
strFile = "filename.pptm"
strSave = "newFilename"
Set pptPres = pptApp.Presentations.Open(strPath & strFile, False, True, True)
Application.DisplayAlerts = False
On Error GoTo Errorhandler_tryAgain
tryAgain:
pptApp.DisplayAlerts = ppAlertsNone
strSave = "Test123"
pptPres.SaveAs strPath & strSave & ".pptx"
pptPres.Close
Exit Sub
Errorhandler_tryAgain:
Debug.Print "Errorhandler_tryAgain was opened!"
Application.Wait DateAdd("s", 1, Now) 'delay in seconds
GoTo TryAgain
First:
Even though I turned the DisplayAlerts
off this one keeps popping up. However I can not easily reproduce this error. It occurs sometimes. Openening, closing and saving *.pptx
files is part of a loop and surprisingly this error does not reoccur at the same file but it reoccurs about 2 times in a loop with 70 >files.
Second:
When I manually click enter the RuntimeError 70: Permission Denied
is thrown. But then the VBE goes into the debug mode and my Errorhandler is not handling it. The Errohandler is an infinitive loop as I am saving the file on a server and sometimes it fails to save. However when I manually tried to save the document (both, on the server and on the desktop) I got the same "PowerPoint-Error while saving the file." MsgBox
.
现在我的问题是如何摆脱保存错误(这似乎是不可能的)或如何抑制该错误,以便我的宏不会在每次发生时停止。就像我想运行宏过夜一样。
如果有人曾经遇到过这样的事情并且可以帮助我,我会很高兴。
提前致谢
Application.Wait 暂停所有 Excel activity 包括宏的执行。它对于解决时序问题不是很有用。当我想增加一点时间以便程序可以完成 I/O 或剪贴板任务时,睡眠是更好的选择。先添加声明:
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
在问题陈述之前添加错误捕获,包括在问题之后重置错误处理的陈述:
TryCut1:
On Error GoTo TooFast1
objShape.TextFrame2.TextRange.Cut
On Error GoTo -1
然后添加这个用于错误处理。它等待 10 毫秒,然后重试:
TooFast1:
Sleep 10
Resume TryCut1
遵循这两点,你应该没问题...
保存时请注明文件格式。例如 pptPres.SaveAs strPath & strSave & ".pptx",24 '<~~ ppSaveAsOpenXMLPresentation
。还要确保 strPath & strSave & ".pptx"
是您想要的字段的确切名称。否则相应地调整变量。
始终在发出保存(或另存为)语句之后和 .Close
语句之前添加 DoEvents
,以便 Excel 有足够的时间完成它任务。
在这被标记为重复之前: 它不是,因为所有其他保存错误似乎都得到了不同的 MsgBox
。
我正在编写一个从 Excel 打开和关闭 PowerPoint 演示文稿的宏。现在我遇到的问题是,当我尝试保存 PowerPoint 文件时,我会弹出一个消息框:
它说:"PowerPoint-Error while saving the file." 我的代码:
Dim pptPres As PowerPoint.Presentation
Dim pptApp As PowerPoint.Application
Set pptApp = New PowerPoint.Application
strPath = "S:\Folderxy\"
strFile = "filename.pptm"
strSave = "newFilename"
Set pptPres = pptApp.Presentations.Open(strPath & strFile, False, True, True)
Application.DisplayAlerts = False
On Error GoTo Errorhandler_tryAgain
tryAgain:
pptApp.DisplayAlerts = ppAlertsNone
strSave = "Test123"
pptPres.SaveAs strPath & strSave & ".pptx"
pptPres.Close
Exit Sub
Errorhandler_tryAgain:
Debug.Print "Errorhandler_tryAgain was opened!"
Application.Wait DateAdd("s", 1, Now) 'delay in seconds
GoTo TryAgain
First:
Even though I turned the
DisplayAlerts
off this one keeps popping up. However I can not easily reproduce this error. It occurs sometimes. Openening, closing and saving*.pptx
files is part of a loop and surprisingly this error does not reoccur at the same file but it reoccurs about 2 times in a loop with 70 >files.Second: When I manually click enter the
RuntimeError 70: Permission Denied
is thrown. But then the VBE goes into the debug mode and my Errorhandler is not handling it. The Errohandler is an infinitive loop as I am saving the file on a server and sometimes it fails to save. However when I manually tried to save the document (both, on the server and on the desktop) I got the same "PowerPoint-Error while saving the file."MsgBox
.
现在我的问题是如何摆脱保存错误(这似乎是不可能的)或如何抑制该错误,以便我的宏不会在每次发生时停止。就像我想运行宏过夜一样。
如果有人曾经遇到过这样的事情并且可以帮助我,我会很高兴。
提前致谢
Application.Wait 暂停所有 Excel activity 包括宏的执行。它对于解决时序问题不是很有用。当我想增加一点时间以便程序可以完成 I/O 或剪贴板任务时,睡眠是更好的选择。先添加声明:
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
在问题陈述之前添加错误捕获,包括在问题之后重置错误处理的陈述:
TryCut1:
On Error GoTo TooFast1
objShape.TextFrame2.TextRange.Cut
On Error GoTo -1
然后添加这个用于错误处理。它等待 10 毫秒,然后重试:
TooFast1:
Sleep 10
Resume TryCut1
遵循这两点,你应该没问题...
保存时请注明文件格式。例如
pptPres.SaveAs strPath & strSave & ".pptx",24 '<~~ ppSaveAsOpenXMLPresentation
。还要确保strPath & strSave & ".pptx"
是您想要的字段的确切名称。否则相应地调整变量。始终在发出保存(或另存为)语句之后和
.Close
语句之前添加DoEvents
,以便 Excel 有足够的时间完成它任务。