Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 157
Default Permission denied when copying file

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?

--
Ian
--


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default Permission denied when copying file

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



"IanC" wrote in message
...
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?

--
Ian
--



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
permission denied SandyR Excel Programming 2 November 16th 05 01:53 PM
error 70 - Permission denied MT Excel Programming 1 May 8th 05 07:20 AM
file delete in Excel receives permission denied by Server 2003 phurst Excel Programming 2 March 10th 05 09:43 PM
Permission to use object denied WilDeliver[_2_] Excel Programming 2 October 22nd 04 07:46 PM
Permission Denied Audrey Ng Excel Programming 1 October 1st 03 07:20 AM


All times are GMT +1. The time now is 02:55 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"