简体   繁体   English

在 Excel VBA 中保存 PowerPoint 文件时出现不可抑制的错误

[英]Insuppressible Error when saving PowerPoint file in Excel VBA

Before this gets marked as a duplicate: It is not, as all the other saving errors seem to get a different MsgBox .在这被标记为重复之前:它不是,因为所有其他保存错误似乎都有不同的MsgBox

I am writing a macro that opens and closes a PowerPoint Presentation from Excel.我正在编写一个宏,用于从 Excel 中打开和关闭 PowerPoint 演示文稿。 Now I have the issue that when I am trying to save the PowerPoint file I get a pop up Message Box:现在我遇到的问题是,当我尝试保存 PowerPoint 文件时,会弹出一个消息框:

错误信息

It says: "PowerPoint-Error while saving the file."它说:“保存文件时出现 PowerPoint 错误。” My code:我的代码:

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.即使我关闭了DisplayAlerts ,这个还是不断弹出。 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.打开、关闭和保存*.pptx文件是循环的一部分,令人惊讶的是,此错误不会在同一个文件中再次出现,而是在包含 70 个 > 文件的循环中再次出现约 2 次。

Second: When I manually click enter the RuntimeError 70: Permission Denied is thrown.第二:当我手动点击进入RuntimeError 70: Permission Denied被抛出。 But then the VBE goes into the debug mode and my Errorhandler is not handling it.但是随后 VBE 进入调试模式并且我的 Errorhandler 没有处理它。 The Errohandler is an infinitive loop as I am saving the file on a server and sometimes it fails to save. Errohandler 是一个不定式循环,因为我将文件保存在服务器上,有时无法保存。 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."但是,当我手动尝试保存文档(在服务器和桌面上)时,我遇到了相同的“保存文件时出现 PowerPoint 错误”。 MsgBox . MsgBox

Now my question is how do I either get rid of the saving error (which seems to be impossible) or how to surppress that error so that my macro does not stop everytime it occurs.现在我的问题是我如何摆脱保存错误(这似乎是不可能的)或如何抑制该错误,以便我的宏不会在每次发生时停止。 As I would like to run the macro overnight.因为我想在一夜之间运行宏。

In case anyone has experienced such a thing before and can help me out I would be very happy.如果有人以前经历过这样的事情并且可以帮助我,我会很高兴。

Thanks in advance提前致谢

Follow these two things and you should be ok...遵循这两件事,你应该没问题......

  1. Mention the File Format while saving.保存时提及文件格式。 For example pptPres.SaveAs strPath & strSave & ".pptx",24 '<~~ ppSaveAsOpenXMLPresentation .例如pptPres.SaveAs strPath & strSave & ".pptx",24 '<~~ ppSaveAsOpenXMLPresentation Also ensure the strPath & strSave & ".pptx" is the extact name of the fiel as you wanted it.还要确保strPath & strSave & ".pptx"是您想要的strPath & strSave & ".pptx"名称。 Else tweak the variables accordingly.否则相应地调整变量。

  2. Always add DoEvents after you issue the save(or save as) statement and before the .Close statement so Excel can get enough time to finish it's tasks.始终在发出保存(或另存为)语句之后和.Close语句之前添加DoEvents ,以便 Excel 可以获得足够的时间来完成它的任务。

Application.Wait suspends all Excel activity including execution of your macro. Application.Wait 暂停所有 Excel 活动,包括执行您的宏。 It's not so useful for fixing timing problems.它对于修复时序问题不是很有用。 When I want to add a little time so that the program can finish I/O or clipboard tasks, Sleep is a better option.当我想增加一点时间以便程序可以完成 I/O 或剪贴板任务时,睡眠是一个更好的选择。 First add a declaration:首先添加一个声明:

Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)

Add error-trapping before the problem statement, including a statement to reset error handling after the problem:在问题语句之前添加错误捕获,包括在问题之后重置错误处理的语句:

TryCut1:
  On Error GoTo TooFast1
  objShape.TextFrame2.TextRange.Cut
  On Error GoTo -1

Then add this for error handling.然后添加它以进行错误处理。 It waits for 10 milliseconds, then tries again:它等待 10 毫秒,然后再次尝试:

TooFast1:
  Sleep 10
  Resume TryCut1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM