file exists question

G

Gil Carter

Hi All,

I am seeking code to find if a file exists on the user's computer. The file
might be named something like "c:/TestFolder/" & variableABC.

So, something like:
If file exists "c:/TestFolder/" & variableABC Then Application.Run
MacroName:="Project.NewMacros.test"
End If

tia,

Gil
 
J

Jezebel

If you don't mind using the FileSystemObject, you can use its FileExists
method. However a lot of programmers spit blood at the suggestion, and it
does require an extra reference.

Alternatively, try reading the file and trap the error you get if it doesn't
exist ...


Dim pFileName as string
Dim pFileExists as boolean
Dim pTemp as long

pFileName = "c:/TestFolder/" & variableABC
On error resume next
pTemp = GetAttr(pFileName)
pFileExists = (Err = 0)
on error goto 0
 
G

Gil Carter

Jez, ... Thank you so very much. Although there was some I did not
understand, the following worked fine. I expect that adding in the variable
will work too.
:) :)
Gil

Sub aaaFileExist()
'
' aaaFileExist macro
'
'
'If FileExists("c:/Test A/Test 1/TestABC.doc") Then Selection.TypeText
Text:="It exists."
'End If

Dim pFileName As String
Dim pFileExists As Boolean
Dim pTemp As Long

pFileName = "c:/Test A/Test 2/Test ABC.doc"
On Error Resume Next
pTemp = GetAttr(pFileName)
pFileExists = (Err = 0)
If pFileExists = True Then Selection.TypeText Text:="The file was in
deed in folder 2."
If pFileExists = False Then Selection.TypeText Text:="The file was not
present in folder 2."
End Sub
 
J

Jezebel

Pleased to help. You shouldn't omit the 'On error goto 0' line -- this turns
error handling back on. Otherwise you'll get no message if there's an error
in the subsequent code.
 
G

Gil Carter

Thanks Jez. Noted.
:)
Gil

Jezebel said:
Pleased to help. You shouldn't omit the 'On error goto 0' line -- this
turns error handling back on. Otherwise you'll get no message if there's
an error in the subsequent code.
 
P

Peter_A_M (NL)

Personally I'd use the Dir function of VBA (search for it in help - it's
strong and useful).

You just need:

If Dir("c:/TestFolder/" & variableABC)<> "" Then Application.Run

Dir returns the name of the file when it exists or an empty string when not.

Other possible uses of Dir:
Dir may be used with just a path or with wildcards, e.g.
"c:\TestFolder\a*.doc".
Also the use in loops can be very practical (Dir without argument returns
the next filename within the given range):

varTest = Dir("c:\TestFolder") 'or: Dir("c:\TestFolder\a*.doc")
do until varTest = ""
'perform action
varTest = Dir
loop

Maybe this could be of help to someone!
Greetings,
Peter
 
J

Jezebel

Dir works fine. The advantage of GetAttr() is that it's significantly faster
if the directory has oa lot of entries.
 
G

Gil Carter

Thanks to you both! Works great!
:
Cheers,
Gil
Jezebel said:
Dir works fine. The advantage of GetAttr() is that it's significantly
faster if the directory has oa lot of entries.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top