VBA - copy file - Access denied error message

ericlch16

Active Member
Joined
Nov 2, 2007
Messages
311
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows
Hi

I am using VBA to copy a file to a directory. When the user does not have access to the directory, i want to display an msgbox ("Please contact your administrator!")

But how can i catch the access denied error message so that i can do an if statement to display that msgbox

FileSystemObject.CopyFile sourcefile, destinatationfile

IF access denied msg THEN
msgbox("PLEASE CONTACT YOUR ADMINISTRATOR)
END IF
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
The "Access Denied" occurs in two cases:
- when the destination folder (for that particular user) is read only;
- when the destination folder (for that particular user) is accessible for read/write but the destination file already exists and has the read-only file attribute.
These situations can be handled like this:
VBA Code:
    On Error Resume Next
    fso.CopyFile sourcefile, destinationfile
    If Err.Number <> 0 Then
        If Err.Number = 75 Then
            MsgBox "Access denied"
        Else
            MsgBox "An error occurred..."
        End If
        Err.Clear
    End If
    On Error GoTo 0
 
Upvote 0
The "Access Denied" occurs in two cases:
- when the destination folder (for that particular user) is read only;
- when the destination folder (for that particular user) is accessible for read/write but the destination file already exists and has the read-only file attribute.
These situations can be handled like this:
VBA Code:
    On Error Resume Next
    fso.CopyFile sourcefile, destinationfile
    If Err.Number <> 0 Then
        If Err.Number = 75 Then
            MsgBox "Access denied"
        Else
            MsgBox "An error occurred..."
        End If
        Err.Clear
    End If
    On Error GoTo 0

I did this code.

However, the Error msg I am getting is 70 even though the file was copied successfully. It is not returning 0. Any reason why?
 
Upvote 0
When you copy a file, a number of things actually happen at the file level, including:
- both the source file and the target file are opened;
- the first with read permission, the second with exclusive write permission.
If one of these opening attempts fails, for example if another process has (temporarily) blocked this file, this error code occurs: 70 - Permission Denied. This error therefore has a different cause than error code 75 - Access Denied.
It is recommended to check whether the source and target file have exactly the same content.
 
Upvote 0

Forum statistics

Threads
1,223,317
Messages
6,171,419
Members
452,402
Latest member
siduslevis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top