Results 1 to 3 of 3

Thread: how to avoid message error when a filename is wrong

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2003
    Location
    france
    Posts
    71

    how to avoid message error when a filename is wrong

    I want to test a path before using my code. like if it is wrong i can show a userform for checking a new path

    but i don't see, is there a function for this ?

  2. #2
    Lively Member MileOut's Avatar
    Join Date
    Nov 2001
    Location
    Glasgow
    Posts
    83
    Can't you just use Error Trapping in the routine that finds the file?


    I don't know the exact code but here's the idea:

    i.e.

    Code:
    Public Function IsFile(byVal strPath as String) As Boolean
    
        On Error Goto Err_IsFile
    
        check if the file exists
        IsFile = True
    
    Exit_IsFile:
        Exit Function
    
    Err_IsFile:
        IsFile = False
        Resume Exit_IsFile
    
    End Function

    and then, before you run the code to open your file or whatever, something like this

    Code:
    If IsFile(strYourFileName) Then
        ' open file
    Else
        MsgBox "Not a valid file."
    End If

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2003
    Location
    france
    Posts
    71

    resolved

    ok thanks a lotyour code work, but i simplified it a little


    Sub test()
    Dim mypath As String
    On Error GoTo ifmyfileiswrong

    Workbooks.Open filename:=Range("J2").Value
    ' my stuff if no error

    ifmyfileiswrong:
    ' my stuff if error

    End Sub


    thanks a lot


Posting Permissions

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



Click Here to Expand Forum to Full Width