使用 VBScript 脚本检测并修复损坏的 Powerpoint

使用 VBScript 脚本检测并修复损坏的 Powerpoint

我有一个类似这样的 .VBS 脚本,它循环遍历文件夹并保存 PowerPoint 的标题幻灯片

Dim InputFolder : InputFolder = "C:\........\"
Dim OutputFolder : OutputFolder = "C:\........\"
Dim ObjFolder : Set ObjFolder = CreateObject("Scripting.fileSystemObject").GetFolder(InputFolder)

Dim Extension : Extension = "PNG"
Dim PixelWidth : PixelWidth = 1024
Dim PixelHeight : PixelHeight = 768

Dim ObjPPT : Set ObjPPT = CreateObject("PowerPoint.Application")
Dim ObjSPresentation
Dim ObjSlide

For Each File In ObjFolder.Files
    if (File.Type = "Microsoft PowerPoint Presentation") Then

        ' Open PPT
        ObjPPT.Presentations.Open File
        Set ObjPresentation = ObjPPT.ActivePresentation

        ' Select title slide
        Set ObjSlide = ObjPresentation.Slides(1)

        ' Export slide
        ObjSlide.Export OutputFolder & ObjPresentation.Name & "." & Extension, _
            Extension, _
            PixelWidth, _
            PixelHeight

        ' Close PPT
        ObjPresentation.Close
    End If
Next

ObjPPT.Quit

它运行正常,直到到达特定文件时引发错误:

C:\........\ppt.vbs(17,4) (null): Unspecified error

当我打开演示文稿时,会出现这个对话框

PowerPoint found a problem with content in C:\broken.pptx.
PowerPoint can attempt to repair the presentation.

If you trust the source of this presentation, click Repair.

修复它没问题。我的问题是,我是否可以让脚本每次都检查这一点,并在必要时自动修复。每当发现损坏的 PPT 文件时都必须重新启动脚本,这有点违背了自动化的目的。

答案1

您可以尝试使用on error

因此,不要:

ObjPPT.Presentations.Open File

尝试类似:

On Error Resume Next
ObjPPT.Presentations.Open File
If Err.Number <> 0 then
  'handle error opening file or emit log to handle manually etc
Endif
On Error Goto 0

您还可以使用标签和goto跳转到文件循环的末尾,或者您可以简单地将循环的其余部分放在条件 where 中Err.Number = 0

有关详细信息,请查看 MS 文档或参阅以下示例:https://stackoverflow.com/questions/24317840/understanding-on-error-in-vbscript

相关内容