Results 1 to 6 of 6
  1. #1
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Silicon Valley, California, USA
    Posts
    273
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Check if two files exist (Office 2000 VBA)

    Hi everybody,

    I have a form with two text boxes for specifying path-and-filename., and a "Compare" button. Normally, when "Compare" is pressed, each of the two files is imported into a sheet in Excel.

    I would like my code to check first and make sure both files actually exist before moving on to importing either one. What is a good way to go about this?

    Thanks!

  2. #2
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Silicon Valley, California, USA
    Posts
    273
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Check if two files exist (Office 2000 VBA)

    Andrew,

    Many thanks! The code within your function is a one-liner, so I think I will simply use two IsValidPath function calls with AND. I wasn't aware of the existence of teh IsValidPath function. Thanks again!

  3. #3
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Check if two files exist (Office 2000 VBA)

    Jim,

    Ooops Jim, my mistake. IsValidPath should read PathExists, or vice versa take your pick. there is no such inbuilt function, sorry. I will amend the post. I just type it in and was not thinking straight.

    Andrew

  4. #4
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Check if two files exist (Office 2000 VBA)

    The following code will return True if the path passed to it actually exists.<pre>Function PathExist(strpath As String) As Boolean
    PathExist= Dir(strpath, vbNormal) <> ""
    End Function</pre>

    you could use <pre>If PathExist(FileName1) And PathExist(FileName2) Then
    Do Whatever .......</pre>


    Andrew C

  5. #5
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Silicon Valley, California, USA
    Posts
    273
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Check if two files exist (Office 2000 VBA)

    Wups! Thanks for the update! (After putting my foot in my mouth a few times, now before I post any code, I try it myself. It also saves me from typo-s.)

  6. #6
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Silicon Valley, California, USA
    Posts
    273
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Check if two files exist (Office 2000 VBA)

    Geeze. I misread your correction, too, and didn't pay enough attention to the original code. Here is what I did:

    <pre>If Dir(.txtPath1.Text, vbNormal) = "" Or _
    Dir(.txtPath2.Text, vbNormal) = "" Then
    ' Show error message
    Else
    ' Do something
    End If
    </pre>


    Weirdly, if the text boxes are empty when I click the button, the Dir function returns the first file name in the current directory. Therefore, I also have to check if the text box text lengths are zero before checking with the Dir function (that is, if .txtPath1.Text already equals "", the Dir function returns the first file name in the current directory, not ""). Also, I locked the text boxes, so they can only be populated with the output of the GetOpenFileName function, triggered by a "Browse..." button.

Posting Permissions

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