Consulting

Results 1 to 14 of 14

Thread: Path/Access Error

  1. #1
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location

    Path/Access Error

    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-...tml#post144981
    Greg

    Visit my website: http://gregmaxey.com

  2. #2
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location
    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.
    .

  3. #3
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location
    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.


  4. #4
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location
    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
    Last edited by Kilroy; 09-17-2019 at 09:28 AM.

  5. #5
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    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.
    Greg

    Visit my website: http://gregmaxey.com

  6. #6
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location
    Doesn't the line "oFSO.DeleteFolder "C:\Demo Issue" & m_arrSubFolders(lngIndex)" already do that?

    update: that was a silly question.
    Last edited by Kilroy; 09-17-2019 at 12:32 PM.

  7. #7
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Kilroy,

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

    Visit my website: http://gregmaxey.com

  8. #8
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location
    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?

  9. #9
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  10. #10
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    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
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  11. #11
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    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.
    Greg

    Visit my website: http://gregmaxey.com

  12. #12
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location
    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.

  13. #13
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location
    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...…

  14. #14
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •