Ark68
Well-known Member
- Joined
- Mar 23, 2004
- Messages
- 4,634
- Office Version
- 365
- 2016
- Platform
- Windows
I am getting a "Path/File access error with the line of code highlighted red below. The procedure is meant to search a drive location to see if a particular folder exists. If it doesn't, to create it. Otherwise, open it in Windows Explorer.
The error I feel is happening because it'ts trying to create a folder that alrerady exists. The value for ffound is reporting "" whether the folder exists or not.
So, as an example ... suppose txt_model equals "Waterloo". The path for which this folder would exist, it it exists, is "O:\IFM\W\"
In the case "Waterloo" doesn't exist (check if fldpath "O:\IFM\W\Waterloo" exists), ffound returns "", and the folder is created.
However, what is happening is ... "Waterloo" already exists in ""O:\IFM\W\", however, ffound returns "" so the code tries to create it ... again ... and fails. What is wrong with my code that ffound is returning unexpected results?
The error I feel is happening because it'ts trying to create a folder that alrerady exists. The value for ffound is reporting "" whether the folder exists or not.
So, as an example ... suppose txt_model equals "Waterloo". The path for which this folder would exist, it it exists, is "O:\IFM\W\"
In the case "Waterloo" doesn't exist (check if fldpath "O:\IFM\W\Waterloo" exists), ffound returns "", and the folder is created.
However, what is happening is ... "Waterloo" already exists in ""O:\IFM\W\", however, ffound returns "" so the code tries to create it ... again ... and fails. What is wrong with my code that ffound is returning unexpected results?
Rich (BB code):
Public Sub open_folder(txt_model As String)
Dim modelini As String
Dim ffound ' As String
Dim response
Dim fldpath As String
Stop
If Right(txt_model, 1) = "." Then txt_model = Left(txt_model, (Len(txt_model) - 1))
modelini = Left(txt_model, 1)
fldpath = "O:\IFM\" & modelini & "\" & txt_model & "\"
ffound = Dir(fldpath)
If ffound = "" Then 'folder not found. Opt to create one.
On Error Resume Next
MkDir "O:\IFM\" & modelini 'what happens here if this directory already exists, which it always will?
On Error GoTo 0
MkDir fldpath
MsgBox "Folder for " & txt_model & " created." & Chr(13) & fldpath, vbInformation, "SUCCESS"
End If
Shell "explorer.exe /root," & fldpath, vbNormalFocus
End Sub