Permission denied when copying file

I

IanC

The following code produces a permission denied error when trying to copy a
file.

Private Sub ReplaceDep(sCurPath, sTPPath)
Dim oFileSysObj As Object
Set oFileSysObj = CreateObject("Scripting.FileSystemObject")
' Following line gives "Permission denied (Error 70)"
oFileSysObj.CopyFile sCurPath & "\Deploy2.bat", sTPPath
End Sub

sCurPath and sTPPath are both valid paths and neither have any write
restrictions. The file to be copied exists, is not protected in any way and
is not open.

I've tried replacing the variable with hard coded paths. Out of desparation,
I've tried adding ", True" at the end of the line to force overwriting, even
though the filename does not exist in the destination folder and True is
default for the argument anyway.

I've never used CopyFile before so I may be missing something really obvious
(though not to me).

Using other code I am able to save an open XL file to the same folder
(sTPPath), so there's no problem writing to the folder with VBA.

Any ideas?
 
R

RB Smissaert

Try with this API code:

Option Explicit
Private Type SHFILEOPSTRUCT
hWnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAborted As Boolean
hNameMaps As Long
sProgress As String
End Type
Private Const FO_MOVE As Long = &H1
Private Const FO_COPY As Long = &H2
Private Const FO_DELETE As Long = &H3
Private Const FO_RENAME As Long = &H4
Private Const FOF_SILENT As Long = &H4
Private Const FOF_RENAMEONCOLLISION As Long = &H8
Private Const FOF_NOCONFIRMATION As Long = &H10
Private Const FOF_SIMPLEPROGRESS As Long = &H100
Private Const FOF_ALLOWUNDO As Long = &H40
Private Declare Function SHFileOperation _
Lib "shell32" _
Alias "SHFileOperationA" _
(lpFileOp As SHFILEOPSTRUCT) As Long

Function FileActions(sSource As String, _
sDestination As String, _
Optional iMoveCopyDeleteRename As Integer = 2, _
Optional bSilent As Boolean = True, _
Optional bNoFilenames As Boolean, _
Optional bNoConfirmDialog As Boolean = True, _
Optional bRenameIfExists As Boolean, _
Optional bAllowUndo As Boolean) As Long

'adapted code from Randy Birch at:
'http://vbnet.mvps.org/index.html?code/shell/shdirectorycopy.htm
'----------------------------------------------------------------

'iMoveCopyDeleteRename has these option:
'1 for moving
'2 for copying
'3 for deleting
'4 for renaming

'bSilent: if FALSE the file progress will show
'bNoFilenames: if TRUE no file names will show in the progress
'bNoConfirmDialog: if TRUE there will be no warning when
'overwriting files
'bRenameIfExists: if TRUE there will be renaming if there were
'going to be files overwritten
'bAllowUndo: this applies when deleting files(3) is choosen
'if TRUE the files will go to the Recycle bin

'will return 0 if successful
'----------------------------------------------------------------

Dim FOF_FLAGS As Long
Dim SHFileOp As SHFILEOPSTRUCT

'terminate the folder string with a pair of nulls
sSource = sSource & Chr$(0) & Chr$(0)

sDestination = sDestination & Chr$(0) & Chr$(0)

'determine the user's options selected
FOF_FLAGS = BuildBrowseFlags(bSilent, _
bNoFilenames, _
bNoConfirmDialog, _
bRenameIfExists, _
bAllowUndo)

'set up the options
With SHFileOp
.wFunc = iMoveCopyDeleteRename
.pFrom = sSource
.pTo = sDestination
.fFlags = FOF_FLAGS
End With

'and perform the chosen copy or move operation
FileActions = SHFileOperation(SHFileOp)

End Function

Function BuildBrowseFlags(bSilent, _
bNoFilenames, _
bNoConfirmDialog, _
bRenameIfExists, _
bAllowUndo) As Long

Dim flag As Long

'these can be multiple
If bSilent Then
flag = flag Or FOF_SILENT
End If

If bNoFilenames Then
flag = flag Or FOF_SIMPLEPROGRESS
End If

If bNoConfirmDialog Then
flag = flag Or FOF_NOCONFIRMATION
End If

If bRenameIfExists Then
flag = flag Or FOF_RENAMEONCOLLISION
End If

If bAllowUndo Then
flag = flag Or FOF_ALLOWUNDO
End If

BuildBrowseFlags = flag

End Function


And you would use it like this:

FileActions sCurPath & "\Deploy2.bat", sTPPath, 2

I know it is a lot more code, but it gives a lot of options and it may solve
your permission problem.


RBS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top