+ Reply to Thread
Results 1 to 10 of 10

Check to see if a file exists

  1. #1
    Forum Contributor
    Join Date
    04-27-2006
    Location
    Cayman Islands
    Posts
    379

    Check to see if a file exists

    Hey guys,

    I have an excel spreadsheet used to populate word documents based on a template file that is loaded like this....

    Please Login or Register  to view this content.
    How can I check to see if this file exists before continuing with the macro as my colleagues tend to keep moving the spreadsheet to wherever they want (something I want to avoid as I need to keep central control)?

    Any help appreciated.... !

  2. #2
    Forum Expert
    Join Date
    09-09-2005
    Location
    England
    MS-Off Ver
    2007
    Posts
    1,500
    I would put the code below in a function as it is and then run it in your sub

    http://www.excelforum.com/showthread...ht=file+exists

    Please Login or Register  to view this content.
    Regards

    Dav
    Last edited by Dav; 11-08-2007 at 09:14 AM.

  3. #3
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Smile

    Good afternoon ChrisMattock

    This function will return a True or False answer in the variable FileExist - fn is the filename you are wanting to check.

    Please Login or Register  to view this content.
    HTH

    DominicB
    Please familiarise yourself with the rules before posting. You can find them here.

  4. #4
    Forum Contributor
    Join Date
    04-27-2006
    Location
    Cayman Islands
    Posts
    379
    Hey guys, tried both methods and it kept returning a Flase state, even when the file did exist....

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Any ideas?
    Last edited by ChrisMattock; 11-08-2007 at 11:57 AM.

  5. #5
    Forum Expert
    Join Date
    09-09-2005
    Location
    England
    MS-Off Ver
    2007
    Posts
    1,500
    what if you put the directory at the beginning of the path?
    something like

    "C:\Templates\Template_LOA.dot"

    Regards

    Dav

  6. #6
    Forum Contributor
    Join Date
    04-27-2006
    Location
    Cayman Islands
    Posts
    379
    Unfortunately not really possible as it gets moved a lot so the path is only relative to the workbook - also surely that shouldn't make a difference as VBA understands ThisWorkbook.Path at other places in the code, most notabley in a few lines time when that file is loaded up.

  7. #7
    Forum Expert
    Join Date
    09-09-2005
    Location
    England
    MS-Off Ver
    2007
    Posts
    1,500
    Please Login or Register  to view this content.
    Works for me. Dominics code is shorter than mine so better
    is the directory and the file name returning exactly what you expect, so with the above ammend ment of the file, is this what you are looking for an d in the right place?

    Regards

    Dav

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Chris

    Have you checked you are working with the right directory and filename?

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

    Did you add a folder "Templates" to your default workbook directory? Or are you trying to access "Templates" in the "Office" folder?

    The default path for Excel workbooks is "My Documents". The system path for templates is generally "C:\Program Files\Microsoft Office\Templates".

    Sincerely,
    Leith Ross

  10. #10
    Forum Contributor
    Join Date
    04-27-2006
    Location
    Cayman Islands
    Posts
    379
    Thanks for all your suggestions guys...

    I have found the problem! The template files were hidden, so that my colleagues couldn't get they're hands on them... so now I have unhidden the files and hidden the folder that contains them, now all of the solutions offered above work perfectly. Thanks for your help!

+ 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