+ Reply to Thread
Results 1 to 4 of 4

VBA to check if any files in folder (and subfolders) are open?

Hybrid View

Rerock VBA to check if any files in... 12-12-2014, 11:15 AM
Olly Re: VBA to check if any files... 12-12-2014, 11:43 AM
Rerock Re: VBA to check if any files... 12-12-2014, 01:58 PM
Olly Re: VBA to check if any files... 12-12-2014, 03:48 PM
  1. #1
    Forum Contributor
    Join Date
    07-22-2009
    Location
    Minneapolis, MN
    MS-Off Ver
    2016
    Posts
    220

    VBA to check if any files in folder (and subfolders) are open?

    Hi,
    I have a macro that, based on cell values; creates a new folder, saves the ActiveWorkbook as a new file name in the new folder, copies the contents of the old folder to the new folder (except the old workbook), and deletes the old folder.

    The problem I'm running in to is sometimes there is another file open that is located in the same folder (or occasionally a sub folder), which then causes the macro to crash because you can't copy or move an open file.

    What I need to do is add some code to the existing macro that will check and see if any other files in ThisWorkbook.Path (besides the ActiveWorkbook) are open.

    Let's say the ActiveWorkbook is located in the following folder:

    C:\DIRECTORY_1\DIRECTORY_2\DIRECTORY_3\DIRECTORY_4\DIRECTORY_5\WORKBOOK_LOCATION

    The check would need to accommodate any file type, but typically the folder contents would include xls, doc, pdf, and jpg.

    Also, occasionally there is a folder(s) in ThisWorkbook.Path as well, so it would probably be a good idea to check those folders (and any sub folders) as well.




    Ideally, this would be an IF statement, something like:

    If another file is open in addition to ThisWorkbook Then
    Msgbox"There is another file open, the macro will stop."
    Exit Sub
    Else
    '<PROCEED WITH MACRO>
    End If
    I'm sure this isn't an overly difficult thing to do, I'm just not sure how to go about it.

    Thanks for the help!

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: VBA to check if any files in folder (and subfolders) are open?

    Something like:
    Sub Test()
        Dim stDir As String
        stDir = ThisWorkbook.Path
        If AnyFileLocked(stDir) Then
            MsgBox "File(s) open in " & stDir, vbCritical + vbOKOnly, "Error"
            Exit Sub
        End If
        'continue...
        Debug.Print "No locked files"
    End Sub
    
    Function AnyFileLocked(stDirectory As String) As Boolean
        Dim sFN As Variant
        If Not Right(stDirectory, 1) = "\" Then stDirectory = stDirectory & "\"
        sFN = Dir(stDirectory)
        While sFN <> "" And sFN <> ThisWorkbook.Name
            If FileLocked(stDirectory & sFN) Then
                AnyFileLocked = True
                Exit Function
            End If
            sFN = Dir
        Wend
    End Function
    
    Function FileLocked(stFileName As String) As Boolean
       On Error Resume Next
       Open stFileName For Binary Access Read Write Lock Read Write As #1
       Close #1
       If Err.Number Then
            FileLocked = True
            Err.Clear
        Else
            FileLocked = False
       End If
    End Function
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Forum Contributor
    Join Date
    07-22-2009
    Location
    Minneapolis, MN
    MS-Off Ver
    2016
    Posts
    220

    Re: VBA to check if any files in folder (and subfolders) are open?

    That's awesome, and works perfect for checking the other files that are in the folder!

    How difficult would it be to add on to that to: check IF there are any folders in ThisWorkbook.Path, and check if there are, check if any files open in there as well?

  4. #4
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: VBA to check if any files in folder (and subfolders) are open?

    Okay, try this:

    (note the correction I've made to the original AnyFileLocked function, too)

    Sub Test()
        Dim stDir As String
        stDir = ThisWorkbook.Path
        If AnyFileLockedSub(stDir) Then
            MsgBox "File(s) open in " & stDir, vbCritical + vbOKOnly, "Error"
            Exit Sub
        End If
        'continue...
        Debug.Print "No locked files"
    End Sub
    
    Function AnyFileLockedSub(stDirectory As String)
        Dim fso As Object
        Dim fo, sf
    
        If Not Right(stDirectory, 1) = "\" Then stDirectory = stDirectory & "\"
        On Error Resume Next
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set fo = fso.GetFolder(stDirectory)
        If Not fo Is Nothing Then
            For Each sf In fo.SubFolders
                AnyFileLockedSub = AnyFileLocked(stDirectory & sf)
                If AnyFileLockedSub Then Exit Function
            Next sf
        End If
        AnyFileLockedSub = AnyFileLocked(stDirectory)
    End Function
    
    Function AnyFileLocked(stDirectory As Variant) As Boolean
        Dim sFN As Variant
        If Not Right(stDirectory, 1) = "\" Then stDirectory = stDirectory & "\"
        sFN = Dir(stDirectory)
        While sFN <> ""
            Debug.Print "Checking " & stDirectory & sFN
            If FileLocked(stDirectory & sFN) And sFN <> ThisWorkbook.Name Then
                AnyFileLocked = True
                Exit Function
            End If
            sFN = Dir
        Wend
    End Function
    
    Function FileLocked(stFileName As Variant) As Boolean
       On Error Resume Next
       Open stFileName For Binary Access Read Write Lock Read Write As #1
       Close #1
       If Err.Number Then
            FileLocked = True
            Err.Clear
        Else
            FileLocked = False
       End If
    End Function
    Last edited by Olly; 12-12-2014 at 03:52 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] list of subfolders in folder - without files and sub-subfolders
    By MartyZ in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-11-2022, 10:56 AM
  2. [SOLVED] Reading Excel files in folder/subfolders
    By crakter in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 02-21-2014, 05:48 AM
  3. [SOLVED] Code for deleting all files in a folder & its subfolders
    By Swalih in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-17-2014, 03:21 AM
  4. Moving Files from Folder and Subfolders
    By mvinay in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-30-2014, 01:59 AM
  5. Counting files in folder including subfolders also and folder size
    By mido609 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-12-2012, 03:26 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1