VBA PowerPoint 运行时错误“-2147467259”(80004005):Presentation.Close:失败

问题描述 投票:0回答:1

我有一个从 Excel 运行的宏。此宏打开 PowerPoint 幻灯片,执行一些工作,然后关闭它们。我遇到的问题如下:

此错误消息在宏运行过程中看似随机出现。这个错误无法用

On Error GoTo Label
来防范,并且似乎它与时间相关(基于某些竞争条件),因为在稍后阶段,再次看似随机,该线路将毫无问题地运行。

导致该错误的具体代码如下:

myPresentation.Close
excel vba com powerpoint
1个回答
0
投票

我测试过的东西:

myPresentation.saved=true
myPresentation.Close

这并没有解决问题

myPresentation.saved=true
myPresentation.windows(1).activate
myPresentation.Close

也不起作用。最后我遇到的最稳定的解决方案如下:

#If VBA7 Then
  Private Declare PtrSafe Sub Sleep Lib "Kernel32" (ByVal dwMilliseconds As Long)
#Else
  Private Declare Sub Sleep Lib "Kernel32" (ByVal dwMilliseconds As Long)
#End If


'This function is implemented as a fix for an issue where VBA reports "Presentation.Close: Failed"
'@param pres as Object<PowerPoint.Presentation> - The presentation to close.
'@docs https://stackoverflow.com/questions/78156015/vba-powerpoint-run-time-error-2147467259-80004005-presentation-close-fail/78156016#78156016
Public Sub ClosePresentation(ByVal pres As Object)
  pres.windows(1).Activate
  On Error GoTo EndIt
  While Not pres.Saved
    pres.Saved = True
    Call stdWindow.CreateFromApplication(pres.Application).Quit
    DoEvents
  Wend
EndIt:
End Sub

这利用了库 stdWindow - 这是我维护的一个开源库。最终,这会间接(通过 Windows API)感知到窗口的关闭消息,这似乎非常稳定,因为我认为这正是用户通常关闭窗口的方式。

© www.soinside.com 2019 - 2024. All rights reserved.