Excel VBA: Confirm user has access to network file

What VBA code would I use to confirm the following without opening another file:

1) User can access path on network.
2) User has at least Read access to a file on a network?
3) Sheet exists within a workbook on a network.

Network Path = //HelloWorld/Import/Closed/File/ClosedFile.xls
Who is Participating?
Bill PrewConnect With a Mentor Commented:
Here is how I would approach it.  I created a small function (CanReadFile) that you can use to test if the file can be read.  This should catch any errors, including the file or folder doesn't exist, the file is not accessible, etc.  Then you can call that from your code (sample in Test) and check the return code.  Zero means the file can be accessed, anything else will be the error code that occurred, and you can display that if desired along with a text version of the error code.  Hope this helps...

Option Explicit

Sub Test()

    ' Declare local variables
    Dim intReturn

    ' Test CanReadFile() function, display any error
    intReturn = CanReadFile("c:\temp\zzz.xxx")
    If intReturn <> 0 Then
        Debug.Print "*ERROR* " & intReturn & " = [" & Error(intReturn) & "]"
    End If
End Sub

Function CanReadFile(strPath) As Integer

    ' Declare local variables
    Dim objFSO As Object
    ' Create filesystem object
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    ' Handle errors in code
    On Error Resume Next
    ' Try to copy the file to NUL device
    objFSO.CopyFile strPath, "NUL"
    ' Return status of copy (0 if no error, otherwise error code)
    CanReadFile = Err.Number
    ' Let VBA handle errors
    On Error GoTo 0

End Function

Open in new window

Bill PrewCommented:
There is no way to confirm the existence of a specific sheet name in the workbook without opening it.

ouestqueAuthor Commented:
Makes sense Bill. What VBA would I use to check for at least Read access to a file and file path?
ouestqueAuthor Commented:
Really Cool Bill! Thank you!!
Bill PrewCommented:
Welcome, glad that was helpful.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.