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