代码之家  ›  专栏  ›  技术社区  ›  Selrac

检查网络文件时进程卡住

  •  1
  • Selrac  · 技术社区  · 6 年前

    我发现,对于某些文件,VBA进程停止并打开debug窗口时没有任何错误,只是高亮显示了一行(我在下面指出)。如果我按F5继续,进程将毫无问题地完成。

    FilePath = "\\network\file.pdf"
    If Not Dir(FilePath, vbDirectory) = vbNullString Then
    
        ' The process gets stuck on the next line
        ' I need to add a delay to prevent it to stop like: Application.Wait Now + #12:00:05 AM#
    
        Worksheets(valRef).OLEObjects.Add Filename:=FilePath, Link:=False, DisplayAsIcon:=False
    Else
        ' Do something else
    End If
    

    这似乎发生在文件非常大的时候。如果我添加一个wait,那么这个过程就可以了,但是当我必须处理很多文件时,我会大大延迟整个过程。有没有办法阻止它停止?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Guest    6 年前

    您可以在下面找到您喜欢的组件:

    'To check if a particular file exists
    'excelFile = False, if it is not an Excel file that is being checked
    Public Function isAnExistingFile(ByVal fileNameStr As Variant, Optional ByVal excelFile As Boolean = True) As Boolean
    Dim wb As Workbook
    
    isAnExistingFile = True
    On Error Resume Next
    If Not VarType(fileNameStr) = vbString Then
        isAnExistingFile = False
    ElseIf Len(fileNameStr) = 0 Then
        isAnExistingFile = False
    ElseIf Len(Dir(fileNameStr)) = 0 Then
        isAnExistingFile = False
    ElseIf ((GetAttr(fileNameStr) And vbDirectory) <> vbDirectory) = False Then
        isAnExistingFile = False
    Else
        If excelFile Then
            Set wb = Application.Workbooks.Open(Filename:=fileNameStr, UpdateLinks:=0, ReadOnly:=True)
            If wb Is Nothing Then isAnExistingFile = False
            If Not wb Is Nothing Then
                wb.Close False
                Set wb = Nothing
            End If
        End If
    End If
    Err.Clear: On Error GoTo 0
    
    End Function