+ Reply to Thread
Results 1 to 6 of 6

VBA to check if folder is open

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    VBA to check if folder is open

    I am using the pushbuttion() event in excel 2000 to follow hyperlink and open a folder. Well of course if the folder is already open the VBA throws an error. Is there a way for me to check if the folder is already open, and if the folder is already open, then DO Nothing?

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,523

    Re: VBA to check if folder is open

    Hi,
    What is the code in your button?

  3. #3
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: VBA to check if folder is open

        Dim OpenFold
         
        OpenFold = UserForm1.txtBox1.Text
      
        strFolder = "L:\" & OpenFold
        FollowHyperlink Address:=strFolder, NewWindow:=True
    Last edited by jo15765; 04-20-2012 at 11:00 AM.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: VBA to check if folder is open

    Hello jo15765,

    Try this revision to your code...
    
        Dim OpenFold As Variant
        Dim oShell As Object
        Dim Wnd As Object
         
            OpenFold = UserForm1.txtBox1.Text
            strFolder = "L:\" & OpenFold
      
            Set oShell = CreateObject("Shell.Application")
            
            For Each Wnd In oShell.Windows
                If Wnd.Document.Folder.Self.Path = OpenFold Then
                    Exit Sub    ' Folder is open - exit this Sub
                Else
                    FollowHyperlink Address:=strFolder, NewWindow:=True
                End If
            Next Wnd
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Registered User
    Join Date
    07-16-2013
    Location
    UK
    MS-Off Ver
    Excel 2000
    Posts
    1

    Re: VBA to check if folder is open

    Quote Originally Posted by Leith Ross View Post
    Hello jo15765,

    Try this revision to your code...
    
        Dim OpenFold As Variant
        Dim oShell As Object
        Dim Wnd As Object
         
            OpenFold = UserForm1.txtBox1.Text
            strFolder = "L:\" & OpenFold
      
            Set oShell = CreateObject("Shell.Application")
            
            For Each Wnd In oShell.Windows
                If Wnd.Document.Folder.Self.Path = OpenFold Then
                    Exit Sub    ' Folder is open - exit this Sub
                Else
                    FollowHyperlink Address:=strFolder, NewWindow:=True
                End If
            Next Wnd
    Hi I am not sure if you can help but I have used the code above , but instead of having a message box pop up saying the folder is open I would like to bring the open folder to the front

    
    Dim OpenFold As Variant
    Dim retval
        Dim oShell As Object
        Dim Wnd As Object
        Set oShell = CreateObject("Shell.Application")
            OpenFold = "F:\media\television\"
            strFolder = "F:\media\television\"
            Set oShell = CreateObject("Shell.Application")
            For Each Wnd In oShell.Windows
                If Wnd.Document.Folder.Self.Path = OpenFold Then
                    MsgBox "Folder is open"
                    Exit Sub    ' Folder is open - exit this Sub
                Else
                End If
            Next Wnd
                     retval = Shell("C:\WINDOWS\explorer.exe """ & OpenFold, 1)

  6. #6
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: VBA to check if folder is open

    That got it! Thank you!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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