+ Reply to Thread
Results 1 to 4 of 4

Open file, if file exists

  1. #1
    Registered User
    Join Date
    12-18-2007
    Location
    London, UK
    MS-Off Ver
    MS Office 2007 & 2010
    Posts
    19

    Question Open file, if file exists

    Hi,

    I am trying to write a script that will run a macro on a number of files if they exist.
    If they do not, it will look for another the next pre-defined file and so on, until all 16 files have been searched for and the macro run, where applicable.

    e.g.

    If file "C:\My Documents\Book1.xls" exists then run macro
    then
    If file "C:\My Documents\Book2.xls" exists then run macro

    But am not sure how to combine the "If file exists" and "if file does not exist" functionality.

    Anyone able to assist?

  2. #2
    Valued Forum Contributor Sadath31's Avatar
    Join Date
    03-02-2011
    Location
    Dammam, Saudi Arabia
    MS-Off Ver
    Office 365
    Posts
    452

    Re: Open file, if file exists

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    12-18-2007
    Location
    London, UK
    MS-Off Ver
    MS Office 2007 & 2010
    Posts
    19

    Re: Open file, if file exists

    Thanks Sadath31, but rather than produce a message box to inform me the file isn't there, I would like it to look for the next file?
    In addition, if the file is there, run the macro, then look for the next file and repeat.

    I'm pressuming that this should be run in some sort of loop?

    Many thanks

  4. #4
    Valued Forum Contributor Sadath31's Avatar
    Join Date
    03-02-2011
    Location
    Dammam, Saudi Arabia
    MS-Off Ver
    Office 365
    Posts
    452

    Re: Open file, if file exists

    Assume you have filenmae with extension (.xls) in range A1:A100
    (sorry I am not tested the macro)

    Sub FileOpen()
    For Each Fname In Range("A1:A100")
    With Application.FileSearch
    .NewSearch
    .LookIn = "D:\"
    .Filename = Fname
    .Execute
    If .Execute() > 0 Then
    Call XYZ 'Macro name to be run
    Exit Sub
    End If
    End With
    Next Fname
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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