Results 1 to 6 of 6

Check if file is open and open if closed

Threaded View

Jockster Check if file is open and... 04-21-2010, 09:13 AM
Andy Pope Re: Check if file is open and... 04-21-2010, 09:32 AM
Jockster Re: Check if file is open and... 04-21-2010, 11:53 AM
elmarko123 Re: Check if file is open and... 04-22-2010, 04:44 AM
Jockster Re: Check if file is open and... 04-22-2010, 07:06 AM
elmarko123 Re: Check if file is open and... 04-22-2010, 08:18 AM
  1. #6
    Registered User
    Join Date
    04-14-2010
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Check if file is open and open if closed

    Quote Originally Posted by Jockster View Post
    Thanks elmarko,
    Have tried your code but when I changed the file name to check the 'else' part of the function, I get the debug window with "Run-time error '53': File not found". So, even with the 'on error resume next' and 'on error goto 0' in place, the code still halts and displays the debug window.
    Is there something in place which is cancelling out the error handling 'override'?
    The function part of the code can be left as it is,

    You only need to edit below the line for the path and what you want it do if it does, or does not exist

    Option Explicit
     
    Function FileOrDirExists(spath As String) As Boolean 'boolean is a true or false statement.
    
        Dim iTemp As Integer 'integer is a numeric value which also allows decimal places.
         
    On Error Resume Next
        iTemp = GetAttr(spath)
          
    If iTemp > 1 Then
    
        FileOrDirExists = True 'if there are no errors on obtaining the pathname then the file exists
            Else
        FileOrDirExists = False 'anything else then the file does not exist & can be created
       
    End If
    
    On Error GoTo 0 
    
    End Function
    
    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 
    
    Sub Exists()
    
    Dim h As String
    
    Dim spath As String
    
         
        spath = "   Put Your File Path in this gap between the speech marks    " 
    
                       (Example) =    "C:\Documents and Settings\User\Desktop\Data Set.xls"
    
        If FileOrDirExists(spath) Then
            ****What you want it do do if it does exist******
        Else
             ****What you want it do do if it does NOT exist******
        End If
        
    End Sub
    Last edited by elmarko123; 04-22-2010 at 08:25 AM.

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