如何检查工作簿是否已损坏

时间:2017-11-14 08:35:54

标签: excel vba sharepoint

我的问题是,如何检查从sharepoint下载的工作簿是否已损坏且是否可以打开。嗯,最简单的方法是尝试打开它。我已经写了一个函数:

Function CanBeOpen(sPath As String) As Boolean
Dim wb As Workbook
On Error Resume Next

With Application
    .EnableEvents = False
    .DisplayAlerts = False

    Set wb = .Workbooks.Open(sPath, False, True)
    .SendKeys "~", True 'To close progressbar when opening

    Select Case TypeName(wb)
        Case Is = "Nothing"
            CanBeOpen = False
        Case Is = "Workbook"
            CanBeOpen = True
        Case Else
            CanBeOpen = False
    End Select

    wb.Close savechanges:=False
    .EnableEvents = True
    .DisplayAlerts = True
End With

Set wb = Nothing
On Error GoTo 0
End Function

然而,这不是非常有效的方法。如果文件很大,打开它需要大量的时间。我想知道是否有人知道更快更优雅的解决方案。

0 个答案:

没有答案