pteehan
asked on
How to trap 'Access Denied' with CopyFile
Hello,
I am using the CopyFile API function to copy a file from the user's machine to a network directory. Under most circumstances it should work fine, but I have identified two conditions where it might fail and I need to be able to trap them:
1. the destination file already exists (in which case I need to prompt them for the next step - it may be overwrite, it may be rename, it may be cancel, up to them)
2. they don't have write access to this network directory (in which case I need to throw an error and bail)
All I have been able to do so far is test if CopyFile(parameters) = 0, but this doesn't tell me very much.
By the way here is the declaration in case it helps.
Private Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" _
(ByVal lpExistingFileName As String, ByVal lpNewFileName As String, _
ByVal bFailIfExists As Long) As Long
Thanks for your help
I am using the CopyFile API function to copy a file from the user's machine to a network directory. Under most circumstances it should work fine, but I have identified two conditions where it might fail and I need to be able to trap them:
1. the destination file already exists (in which case I need to prompt them for the next step - it may be overwrite, it may be rename, it may be cancel, up to them)
2. they don't have write access to this network directory (in which case I need to throw an error and bail)
All I have been able to do so far is test if CopyFile(parameters) = 0, but this doesn't tell me very much.
By the way here is the declaration in case it helps.
Private Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" _
(ByVal lpExistingFileName As String, ByVal lpNewFileName As String, _
ByVal bFailIfExists As Long) As Long
Thanks for your help
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Put this abel in that form.
on error goto ErrHanlder:
ErrHandler:
If err.number = 70 or err.number = 75 then
'Ignores Access Denied
end if
on error goto ErrHanlder:
ErrHandler:
If err.number = 70 or err.number = 75 then
'Ignores Access Denied
end if
ASKER
Thanks AlexFM.
GetLastError didn't seem to work but from the article you linked I noticed Err.LastDllError - this is what I needed. Exactly what I was looking for!
GetLastError didn't seem to work but from the article you linked I noticed Err.LastDllError - this is what I needed. Exactly what I was looking for!
Public Declare Function GetLastError Lib "kernel32" Alias "GetLastError" () As Long
If it returns 5, this means Access is Denied.