Establishing if a file exists

S

Sue

I just got the Application.File.Search running nicely when I find Microsoft
is deleting that facility. I now can not get FileExists to work! (I am always
a jump behind!) All I need to know is if the file is in the current
directory. If it is not, the user will just get a message to find it
themselves and shift it. I do not understand the object required. Could
someone help with a bit of code that will findout if "Test.doc" is in the
current directory? I also need to check for "Data.txt" in the current
directory. Many thanks, Sue
 
G

Graham Mayor

How about

Dim sDoc As String
Dim sPath As String
sDoc = "Test.doc"
sPath = "D:\My Documents\Test\"
If (Dir(sPath & sDoc) > "") Then
MsgBox "Exists"
Else
MsgBox "Does not exist"
End If


which will find either document - and with the user prompt

Dim sPath As String
Dim sDoc As String
Dim oDoc As Document
Dim fDialog As FileDialog
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
sPath = "D:\My Documents\Test\"
sDoc = "Test.doc"
If (Dir(sPath & sDoc) > "") Then 'File exists
Set oDoc = Documents.Open(sPath & sDoc)
Else 'File does not exist
Set dDialog = Application.FileDialog(msoFileDialogFilePicker)
With dDialog
.title = "Locate the document " & sDoc
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then 'User has cancelled
sCancel = MsgBox("Cancelled By user.", _
vbOK, "No Document")
Application.ScreenUpdating = True
Exit Sub
Else 'User has not cancelled
Set oDoc = Documents.Open(dDialog.SelectedItems.Item(1))
End If
End With
End If


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
K

Karl E. Peterson

Graham said:
How about

Dim sDoc As String
Dim sPath As String
sDoc = "Test.doc"
sPath = "D:\My Documents\Test\"
If (Dir(sPath & sDoc) > "") Then
MsgBox "Exists"
Else
MsgBox "Does not exist"
End If

Shouldn't use Dir() for something like that! Dir is "problematic" in how it handles
the Find handle behind the scenes, and calling it in a utility function like this
can disrupt its use farther up the callstack. A far cleaner FileExists would use
GetAttr instead...

Public Function FileExists(ByVal FileName As String) As Boolean
Dim nAttr As Long
' Grab this files attributes, and make sure it isn't a folder.
' This test includes cases where file doesn't exist at all.
On Error GoTo NoFile
nAttr = GetAttr(FileName)
If (nAttr And vbDirectory) <> vbDirectory Then
FileExists = True
End If
NoFile:
End Function

Public Function FolderExists(ByVal PathName As String) As Boolean
Dim nAttr As Long
' Grab the attributes, test valid values for folder bit.
On Error GoTo NoFolder
nAttr = GetAttr(PathName)
If (nAttr And vbDirectory) = vbDirectory Then
FolderExists = True
End If
NoFolder:
End Function
 

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