由于我无法回复包含 deepfileexists 代码的评论,因此这里修改了代码,以便您可以找到网络路径(因为他回答说他有网络位置)
您需要一个调用 system32 的函数来执行到网络驱动器的直接路径。我从这里得到了代码
继承人代码,在模块顶部插入私有函数,否则它将不起作用。它使功能专门与该模块相关联,如果您想将其打开到整个工作簿,请关闭私有功能。
Private Declare Function SetCurrentDirectoryA Lib "kernel32" _
(ByVal lpPathName As String) As Long
然后是带有修改后代码的函数,以在网络驱动器以 \ 开头时接受该函数
Function deepFileExists(longFileName As String)
' slowly make your way to the deepest folder...
' assuming "\" is used as separator
' you could add some code to replace "/" with "\"...
Dim pathFragment As String, currentDir As String
Dim slash As Integer, lastSlash As Integer
If Left(longFileName, 2) = "\\" Then
slash = InStr(3, longFileName, "\")
Else
slash = InStr(1, longFileName, "\")
End If
lastSlash = 0
pathFragment = Mid(longFileName, 1, slash - 1)
currentDir = CurDir ' save the current directory
If Not Left(pathFragment, 2) = "\\" Then
ChDrive pathFragment ' making sure we have the right drive
ChDir pathFragment & "\" ' be at the root of this drive's directory
Else
SetCurrentDirectoryA (pathFragment)
End If
lastSlash = slash
slash = InStr(slash + 1, longFileName, "\")
While (slash > 0)
pathFragment = ".\" & Mid(longFileName, lastSlash + 1, slash - lastSlash)
If Not Left(longFileName, 2) = "\\" Then
ChDir pathFragment
Else
SetCurrentDirectoryA (pathFragment)
End If
'MsgBox "changing directory to " & pathFragment
lastSlash = slash
slash = InStr(slash + 1, longFileName, "\")
Wend
' now we can look for the file:
Dim a As String
Dim something As String
something = Mid(longFileName, lastSlash + 1)
a = Dir(something)
If Len(a) > 0 Then
deepFileExists = True
Else
deepFileExists = False
End If
End Function