Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to check whether a file exist and whether it is readable

Status
Not open for further replies.

Chrissirhc

Programmer
May 20, 2000
926
GB
Hi I have this code that checks whether a file exists:

Dim fso As Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject

fileExists = True
If fso.fileExists(fileNameLocation) = False Then
fileExists = False
End If

The problem is, I'll try to open the file if it exists, but sometimes a user may be able to see the file, but won't have read access to it. How can I check whether they have read access without actually workbooks.open??? (I need to use a standardised way to open the file... I shouldn't use workbooks.open)

Thanks,

Chris
 
the 'standard' way i use is to just open the file with error handling....

Code:
on error goto errMsg

Workbooks("Path & File.xls").open


on error goto 0
'process stuff here

exit sub
errMsg:
msgbox "you cannot open this file"
end sub

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 
To add to Geoff's response you can have logic for specific errors if you know what they are, e.g.

Code:
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'~  Error handling
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    On Error GoTo MyProcedure_Error
    GoTo MyProcedure_Exit
MyProcedure_Error:
  If Err.Number = 70 Then
    MsgBox ("Special handling for zero length files ")
    Resume Next
  Else
    MsgBox ("Special handling for not error #70 ")
    Resume Next
  End If
MyProcedure_Exit:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top