Could anyone tell me how do I find out if user does not have access rights to an excel file
on error goto label
set a = workbooks.open("some name")
exit sub
label:
msgbox "Current user does not have access rights or file does not exist or _
a document with the same name is already opened in another folder"
end sub
I just used a message box on error of file opening , I have given 3 reasons that may cause the error but I want the specific error to pop out, like if user does not have access rights , the message should contain only no access rights.
I tried to catch the error numbers but all the 3 errors give 1004 number so unable to differentiate.
Though after googling, I used vba.len(vba.dir(filename,vbnormal))=0 to check if file exists or not
Now I need to know if a user has access rights or not to an excel file . How do i know that ? any help is greatly appreciated
I think you basically have the right approach. I would also use error trapping to determine whether the user has access to a file. However, I agree that a non-specific error message is not very useful.
This will display VBA’s error-specific message (even though the error number stays the same):
If you want to detect what exactly the error is and use it e.g. to display your own custom message, then you could do something like this:
Though it is better to check things yourself rather than rely on
Err.Descriptionif possible. Checking whether the file exists directly is possible, e.g like this:I don’t know of an equivalent function to test for user access, so I would fall back on looking at the content of
Err.Description.