VBA:通过使用UNC Path和FileSystemObject拒绝长响应

问题描述:

我尝试使用UNC Path和FileSystemObject获取网络中的目录,但是如果网络目录不可用,则响应需要很长时间。我想这是因为扫描了很多网络,或者在这一点上发送更多ping。 那么是否有一种方法可以在使用FSO之前更快地检查网络目录的存在?VBA:通过使用UNC Path和FileSystemObject拒绝长响应

+0

这是VBA,但这并不重要。 – Kinimod

我已经添加了文件检查(以及它从子例程调用的示例),然后进行文件夹检查。两者都以相同的方式调用,并且都使用Dir函数。我在文件检查中添加了一个错误处理程序,值得为文件夹检查程序添加以下代码:

Sub Main() 
    if FileExists("O:\Filenamehere") = TRUE then 
     'do stuff 
    end if 
End Sub 

'For a file 
Function FileExists(ByVal FullPath As String) As Boolean 
    On Error GoTo Hand 
    If Dir(FullPath) <> "" Then 
     FileExists = True 
    Else 
     FileExists = False 
    End If 
    Exit Function 
Hand: 
    Select Case Err.Number 
     Case 52 
      FileExists = False 
    End Select 
End Function 

'For a directory 
Function DirectoryExists(ByVal FullPath As String) As Boolean 
    If Dir(FullPath, vbDirectory) <> "" Then DirectoryExists = True Else DirectoryExists = False 
End Function