PDA

View Full Version : Path/Access Error



gmaxey
09-16-2019, 05:43 PM
Hi Guys,

Coming here this time with my hand out needing a fish ;-)


I have a process that creates a folder structure on my C drive. Overtime, I put files in the various folders and at times I then need to delete the root folder, sub-folders and all files. I'm having issues with the code as "more times than" not some of the files and folders are not delete and I get RTE 75 Path/Access error. After getting the error, no combination of code (and I've tried all day) will delete the offending folder. However, that folder can be deleted manually with no problems.

Here is the the code that creates the structure, deletes the structure and some code to add files to the folders. If you simply create the structure and immediately delete it, it will delete without issue. However, if you start adding files to the structure then attempt to delete then more often than not here, I get the RTE.

Any help or ideas on how to consistently delete a folder and all subfolders and files is appreciated.


Option Explicit
Dim oFSO As Object, oFolder As Object, oFile As Object
Dim m_strRoot As String
Dim m_arrSubFolders() As String

Sub CreateFolderStructure()
'1. This procedure creates a folder structure on my C drive.
Dim lngIndex As Long
m_arrSubFolders = Split("Case Data|Case Data\Client|Case Data\Counsels|" _
& "Case Data\Misc|Case Templates|Files|Files\Faxes|Files\Letters|Files\Letters\PDF Final|Files\Letters\Client|" _
& "Files\Letters\Client\Drafts|Files\Letters\Opposing Counsel|Files\Letters\Other|Files\Memos|Files\Memos\PDF Final|" _
& "Files\Misc|Files\Pleadings|Files\Pleadings\Draft|Files\Pleadings\PDF", "|")
m_strRoot = "C:\Demo Issue"
CreateFolder m_strRoot
For lngIndex = 0 To UBound(m_arrSubFolders)
CreateFolder m_strRoot & "\" & m_arrSubFolders(lngIndex)
Next lngIndex
End Sub

Sub KillFoldersAndFiles()
'2. This procedure is intended to delete are files and folders in the folder structure created in step 1. _
It works fine if excuted immediately after the folder structure is created (all folders empty with no files).
Dim lngIndex As Long
m_arrSubFolders = Split("Case Data|Case Data\Client|Case Data\Counsels|" _
& "Case Data\Misc|Case Templates|Files|Files\Faxes|Files\Letters|Files\Letters\PDF Final|Files\Letters\Client|" _
& "Files\Letters\Client\Drafts|Files\Letters\Opposing Counsel|Files\Letters\Other|Files\Memos|Files\Memos\PDF Final|" _
& "Files\Misc|Files\Pleadings|Files\Pleadings\Draft|Files\Pleadings\PDF", "|")
Set oFSO = CreateObject("Scripting.FileSystemObject")
For lngIndex = UBound(m_arrSubFolders) To 0 Step -1
If fcnDirExists("C:\Demo Issue\" & m_arrSubFolders(lngIndex)) Then
Set oFolder = oFSO.GetFolder("C:\Demo Issue\" & m_arrSubFolders(lngIndex))
For Each oFile In oFolder.Files
oFile.Delete
DoEvents
Next oFile
DoEvents
oFSO.DeleteFolder "C:\Demo Issue\" & m_arrSubFolders(lngIndex)
End If
Next lngIndex
DoEvents
oFSO.DeleteFolder "C:\Demo Issue"
lbl_Exit:
Exit Sub
End Sub

Sub DemoIssue()
'3. However, if I create and save a file in one of the sub-folders created in step one and then run KillFoldersAndFiles, _
I get a Path/File Access error.
Dim oDoc As Document
m_strRoot = "C:\Demo Issue"
Set oDoc = Documents.Add
If Application.Version > 12# Then
oDoc.SaveAs2 Filename:=m_strRoot & "\Files\Pleadings\PDF\Demo Document", FileFormat:=wdFormatXMLDocumentMacroEnabled
Else
oDoc.SaveAs Filename:=m_strRoot & "\Files\Pleadings\PDF\Demo Document", FileFormat:=wdFormatXMLDocumentMacroEnabled
End If
oDoc.Close
' Set oDoc = Documents.Add
' If Application.Version > 12# Then
' oDoc.SaveAs2 Filename:=m_strRoot & "\Files\Letters\Demo Document", FileFormat:=wdFormatXMLDocumentMacroEnabled
' Else
' oDoc.SaveAs Filename:=m_strRoot & "\Files\Letters\Demo Document", FileFormat:=wdFormatXMLDocumentMacroEnabled
' End If
' oDoc.Close
lbl_Exit:
Set oDoc = Nothing
Exit Sub
End Sub

Sub DirectAttempt()
'After the error occurs, no attempt to delete the offending empty folder will work.
RmDir "C:\Demo Issue\Files\Pleadings\PDF"
End Sub


'Supporting functions
Private Function CreateFolder(ByRef strPath As String)
Dim lngIndex As Long
Dim vPath As Variant
vPath = Split(strPath, "\")
strPath = vPath(0) & "\"
For lngIndex = 1 To UBound(vPath)
strPath = strPath & vPath(lngIndex) & "\"
If Not fcnDirExists(strPath) Then MkDir strPath
Next lngIndex
lbl_Exit:
Exit Function
End Function

Function fcnDirExists(PathName As String) As Boolean
Dim lngTemp As Integer
On Error Resume Next
lngTemp = GetAttr(PathName)
Select Case Err.Number
Case Is = 0: fcnDirExists = True
Case Else: fcnDirExists = False
End Select
'Resume error checking
On Error GoTo 0
lbl_Exit:
Exit Function
End Function


Cross posted at: https://www.msofficeforums.com/word-vba/43414-path-access-error-madness.html#post144981

Kilroy
09-17-2019, 05:20 AM
Greg I have no idea how to help you but I have always had issues deleting files. Each time I have this issue I close outlook and no more issue.
.

Kilroy
09-17-2019, 05:47 AM
Greg:
I ran your code and successfully created the subfolders. "SubCreateFolderStructure()"
I ran the code and successfully created the document. "SubDemoIssue()"
When I an your code to delete the folders "Sub KillFoldersAndFiles()"while having a folder open to watch the process it didn’t work. However I triedit with all open folders closed and it did work perfectly.

Kilroy
09-17-2019, 08:42 AM
I added a close file line in the "kill" and commented out another


Try this:


Option Explicit
Dim oFSO As Object, oFolder As Object, oFile As Object
Dim m_strRoot As String
Dim m_arrSubFolders() As String

Sub CreateFolderStructure()
'1. This procedure creates a folder structure on my C drive.
Dim lngIndex As Long
m_arrSubFolders = Split("Case Data|Case Data\Client|Case Data\Counsels|" _
& "Case Data\Misc|Case Templates|Files|Files\Faxes|Files\Letters|Files\Letters\PDF Final|Files\Letters\Client|" _
& "Files\Letters\Client\Drafts|Files\Letters\Opposing Counsel|Files\Letters\Other|Files\Memos|Files\Memos\PDF Final|" _
& "Files\Misc|Files\Pleadings|Files\Pleadings\Draft|Files\Pleadings\PDF", "|")
m_strRoot = "C:\Demo Issue"
CreateFolder m_strRoot
For lngIndex = 0 To UBound(m_arrSubFolders)
CreateFolder m_strRoot & "\" & m_arrSubFolders(lngIndex)
Next lngIndex
End Sub
Sub KillFoldersAndFiles()
'2. This procedure is intended to delete are files and folders in the folder structure created in step 1. _
It works fine if excuted immediately after the folder structure is created (all folders empty with no files).
Dim lngIndex As Long
m_arrSubFolders = Split("Case Data|Case Data\Client|Case Data\Counsels|" _
& "Case Data\Misc|Case Templates|Files|Files\Faxes|Files\Letters|Files\Letters\PDF Final|Files\Letters\Client|" _
& "Files\Letters\Client\Drafts|Files\Letters\Opposing Counsel|Files\Letters\Other|Files\Memos|Files\Memos\PDF Final|" _
& "Files\Misc|Files\Pleadings|Files\Pleadings\Draft|Files\Pleadings\PDF", "|")
Set oFSO = CreateObject("Scripting.FileSystemObject")
For lngIndex = UBound(m_arrSubFolders) To 0 Step -1
If fcnDirExists("C:\Demo Issue\" & m_arrSubFolders(lngIndex)) Then
Set oFolder = oFSO.GetFolder("C:\Demo Issue\" & m_arrSubFolders(lngIndex))


If oFolder = msoOpenFile Then Close ''''''''''''''''''''I added this line


For Each oFile In oFolder.Files
oFile.Delete
DoEvents
Next oFile
DoEvents
oFSO.DeleteFolder "C:\Demo Issue\" & m_arrSubFolders(lngIndex)
End If
Next lngIndex
DoEvents


'oFSO.DeleteFolder "C:\Demo Issue" ''''''''''''''I commented out this line


lbl_Exit:
Exit Sub
End Sub
Sub DemoIssue()
'3. However, if I create and save a file in one of the sub-folders created in step one and then run KillFoldersAndFiles, _
I get a Path/File Access error.
Dim oDoc As Document
m_strRoot = "C:\Demo Issue"
Set oDoc = Documents.Add
If Application.Version > 12# Then
oDoc.SaveAs2 FileName:=m_strRoot & "\Files\Pleadings\PDF\Demo Document", FileFormat:=wdFormatXMLDocumentMacroEnabled
Else
oDoc.SaveAs FileName:=m_strRoot & "\Files\Pleadings\PDF\Demo Document", FileFormat:=wdFormatXMLDocumentMacroEnabled
End If
oDoc.Close
' Set oDoc = Documents.Add
' If Application.Version > 12# Then
' oDoc.SaveAs2 Filename:=m_strRoot & "\Files\Letters\Demo Document", FileFormat:=wdFormatXMLDocumentMacroEnabled
' Else
' oDoc.SaveAs Filename:=m_strRoot & "\Files\Letters\Demo Document", FileFormat:=wdFormatXMLDocumentMacroEnabled
' End If
' oDoc.Close
lbl_Exit:
Set oDoc = Nothing
Exit Sub
End Sub
Sub DirectAttempt()
'After the error occurs, no attempt to delete the offending empty folder will work.
RmDir "C:\Demo Issue\Files\Pleadings\PDF"
End Sub

'Supporting functions
Private Function CreateFolder(ByRef strPath As String)
Dim lngIndex As Long
Dim vPath As Variant
vPath = Split(strPath, "\")
strPath = vPath(0) & "\"
For lngIndex = 1 To UBound(vPath)
strPath = strPath & vPath(lngIndex) & "\"
If Not fcnDirExists(strPath) Then MkDir strPath
Next lngIndex
lbl_Exit:
Exit Function
End Function
Function fcnDirExists(PathName As String) As Boolean
Dim lngTemp As Integer
On Error Resume Next
lngTemp = GetAttr(PathName)
Select Case Err.Number
Case Is = 0: fcnDirExists = True
Case Else: fcnDirExists = False
End Select
'Resume error checking
On Error GoTo 0
lbl_Exit:
Exit Function
End Function

gmaxey
09-17-2019, 09:40 AM
Kilroy,

I modified you last as follows:


Sub KillFoldersAndFiles()
'2. This procedure is intended to delete are files and folders in the folder structure created in step 1. _
It works fine if excuted immediately after the folder structure is created (all folders empty with no files).
Dim lngIndex As Long
m_arrSubFolders = Split("Case Data|Case Data\Client|Case Data\Counsels|" _
& "Case Data\Misc|Case Templates|Files|Files\Faxes|Files\Letters|Files\Letters\PDF Final|Files\Letters\Client|" _
& "Files\Letters\Client\Drafts|Files\Letters\Opposing Counsel|Files\Letters\Other|Files\Memos|Files\Memos\PDF Final|" _
& "Files\Misc|Files\Pleadings|Files\Pleadings\Draft|Files\Pleadings\PDF", "|")
Set oFSO = CreateObject("Scripting.FileSystemObject")
For lngIndex = UBound(m_arrSubFolders) To 0 Step -1
If fcnDirExists("C:\Demo Issue\" & m_arrSubFolders(lngIndex)) Then
Set oFolder = oFSO.GetFolder("C:\Demo Issue\" & m_arrSubFolders(lngIndex))
If oFolder = msoOpenFile Then Close ''''''''''''''''''''I added this line
For Each oFile In oFolder.Files
oFile.Delete
DoEvents
Next oFile
DoEvents
oFSO.DeleteFolder "C:\Demo Issue\" & m_arrSubFolders(lngIndex)
End If
Next lngIndex
DoEvents
If fcnDirExists("C:\Demo Issue") Then
Set oFolder = oFSO.GetFolder("C:\Demo Issue")
If oFolder = msoOpenFile Then Close
oFSO.DeleteFolder "C:\Demo Issue"
End If
lbl_Exit:
Exit Sub
End Sub

If you comment out:
'oFSO.DeleteFolder "C:\Demo Issue" ''''''''''''''I commented out this line

Then the root folder is not deleted.

The issue is still not resolved. For example, if after running the code to set the folders and create and save a file I go and copy that file to several
of the sub-folders and run the Kill macro, it will always error.

If I close and Word and reopen Word and run the kill again then it seems to work (similar to your OUTLOOK case). Seems there should be some way to make
this work reliably.

Kilroy
09-17-2019, 09:57 AM
Doesn't the line "oFSO.DeleteFolder "C:\Demo Issue" & m_arrSubFolders(lngIndex)" already do that?

update: that was a silly question.

gmaxey
09-17-2019, 12:23 PM
Kilroy,

No. All the folders defined in the array are sub-folders of the root folder C:\Demo Issue

Kilroy
09-17-2019, 01:07 PM
Greg I removed the comment on the "oFSO.DeleteFolder "C:\Demo Issue" & m_arrSubFolders(lngIndex)" line.

I tried the revised code I posted and works here even with the "PDF" folder open. It takes less than 2 seconds. Am I missing something?

gmaxey
09-17-2019, 01:48 PM
Kilroy,

It will work here as well (most of the time) provided before running the Kill, I don't copy and paste more documents into the folders. If I do that and run the kill, then I get the error. Close and reopen Word and it runs and works with no errors.

Here is a slightly modified version in case some adds more sub-folders after creation.


Option Explicit
Dim oFSO As Object, oFolder As Object, oFile As Object
Dim m_strRoot As String
Dim m_arrSubFolders() As String

Sub CreateFolderStructure()
'1. This procedure creates a folder structure on my C drive.
Dim lngIndex As Long
m_arrSubFolders = Split("Case Data|Case Data\Client|Case Data\Counsels|" _
& "Case Data\Misc|Case Templates|Files|Files\Faxes|Files\Letters|Files\Letters\PDF Final|Files\Letters\Client|" _
& "Files\Letters\Client\Drafts|Files\Letters\Opposing Counsel|Files\Letters\Other|Files\Memos|Files\Memos\PDF Final|" _
& "Files\Misc|Files\Pleadings|Files\Pleadings\Draft|Files\Pleadings\PDF", "|")
m_strRoot = "C:\Demo Issue"
CreateFolder m_strRoot
For lngIndex = 0 To UBound(m_arrSubFolders)
CreateFolder m_strRoot & "\" & m_arrSubFolders(lngIndex)
Next lngIndex
End Sub
Sub KillFoldersAndFiles()
'2. This procedure is intended to delete are files and folders in the folder structure created in step 1. _
It works fine if excuted immediately after the folder structure is created (all folders empty with no files).
Dim lngIndex As Long
ReDim Preserve m_arrSubFolders(0)
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder("C:\Demo Issue")
RecursiveFolder oFolder
For lngIndex = UBound(m_arrSubFolders) - 1 To 0 Step -1
If fcnDirExists(m_arrSubFolders(lngIndex)) Then
Set oFolder = oFSO.GetFolder(m_arrSubFolders(lngIndex))
If oFolder = msoOpenFile Then Close
For Each oFile In oFolder.Files
oFile.Delete
DoEvents
Next oFile
DoEvents
oFSO.DeleteFolder m_arrSubFolders(lngIndex)
End If
Next lngIndex
DoEvents
If fcnDirExists("C:\Demo Issue") Then
Set oFolder = oFSO.GetFolder("C:\Demo Issue")
If oFolder = msoOpenFile Then Close
oFSO.DeleteFolder "C:\Demo Issue"
End If
lbl_Exit:
Exit Sub
End Sub
Sub DemoIssue()
'3. However, if I create and save a file in one of the sub-folders created in step one and then run KillFoldersAndFiles, _
I get a Path/File Access error.
Dim oDoc As Document
m_strRoot = "C:\Demo Issue"
Set oDoc = Documents.Add
If Application.Version > 12# Then
oDoc.SaveAs2 Filename:=m_strRoot & "\Files\Pleadings\PDF\Demo Document", FileFormat:=wdFormatXMLDocumentMacroEnabled
Else
oDoc.SaveAs Filename:=m_strRoot & "\Files\Pleadings\PDF\Demo Document", FileFormat:=wdFormatXMLDocumentMacroEnabled
End If
oDoc.Close
Set oDoc = Documents.Add
If Application.Version > 12# Then
oDoc.SaveAs2 Filename:=m_strRoot & "\Files\Letters\Demo Document", FileFormat:=wdFormatXMLDocumentMacroEnabled
Else
oDoc.SaveAs Filename:=m_strRoot & "\Files\Letters\Demo Document", FileFormat:=wdFormatXMLDocumentMacroEnabled
End If
oDoc.Close
lbl_Exit:
Set oDoc = Nothing
Exit Sub
End Sub
Sub DirectAttempt()
'After the error occurs, no attempt to delete the offending empty folder will work.
RmDir "C:\Demo Issue\Files\Pleadings\PDF"
End Sub

'Supporting functions
Private Function CreateFolder(ByRef strPath As String)
Dim lngIndex As Long
Dim vPath As Variant
vPath = Split(strPath, "\")
strPath = vPath(0) & "\"
For lngIndex = 1 To UBound(vPath)
strPath = strPath & vPath(lngIndex) & "\"
If Not fcnDirExists(strPath) Then MkDir strPath
Next lngIndex
lbl_Exit:
Exit Function
End Function

Function fcnDirExists(PathName As String) As Boolean
Dim lngTemp As Integer
On Error Resume Next
lngTemp = GetAttr(PathName)
Select Case Err.Number
Case Is = 0: fcnDirExists = True
Case Else: fcnDirExists = False
End Select
'Resume error checking
On Error GoTo 0
lbl_Exit:
Exit Function
End Function

Sub RecursiveFolder(RootFolder)
Dim oSubFolder
For Each oSubFolder In RootFolder.SubFolders
m_arrSubFolders(UBound(m_arrSubFolders)) = oSubFolder.Path
ReDim Preserve m_arrSubFolders(UBound(m_arrSubFolders) + 1)
Set oFolder = oFSO.GetFolder(oSubFolder)
If oFolder.SubFolders.Count = 0 Then
On Error Resume Next
For Each oFile In oFolder.Files
Debug.Print oFile.Path
Next
End If
RecursiveFolder oSubFolder
Next
lbl_Exit:
Exit Sub
End Sub

Paul_Hossler
09-18-2019, 05:07 AM
FWIW it seems to to what you want for me (assuming I understand :))

However, I don't understand the purpose of this. I don't think FSO and the Folder object works like that




If oFolder = msoOpenFile Then Close

gmaxey
09-18-2019, 05:22 AM
Paul,

I'm not sure. Kilroy suggested that. It seems to work here too, provided I simply create the structure, add the files and delete the structure. However, if I create the structure then use Windows Explorer to copy and paste the added files to other subfolders then run the delete sequence, I get the error. If I close and reopen Word and run the delete again, it works.

Kilroy
09-18-2019, 05:42 AM
Greg,
I ran your code and successfully created the subfolders. "SubCreateFolderStructure()"
I ran the code and successfully created the document. "SubDemoIssue()"
I added a folder and copied and pasted a document into every folder
When I ran the "Sub KillFoldersAndFiles()" everything worked perfectly without opening and closing outlook or Word.

Kilroy
09-18-2019, 05:50 AM
Paul it seemed to me the Path/Access error was because the folder was open however I just tried the kill code without it and with the folder open and it worked so...…

gmaxey
09-18-2019, 05:59 AM
Kilroy,

Thanks for that. I don't know what the issue is here. Here I have to close and reopen Word before the delete process will run without error, if i modify any of the folders after creating and saving the initial documents. One of those perplexing things I suppose. Thanks for your time and interest.

Best Regards,
Greg Maxey

The future doesn't belong to the fainthearted; it belongs to the brave.
~ Ronald Reagan