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
ouestqueAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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


»bp
0
ouestqueAuthor Commented:
Makes sense Bill. What VBA would I use to check for at least Read access to a file and file path?
0
Bill PrewCommented:
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
    Err.Clear
    
    ' 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
    Err.Clear
    On Error GoTo 0

End Function

Open in new window


»bp
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ouestqueAuthor Commented:
Really Cool Bill! Thank you!!
0
Bill PrewCommented:
Welcome, glad that was helpful.


»bp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.