Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
permission denied | Excel Programming | |||
error 70 - Permission denied | Excel Programming | |||
file delete in Excel receives permission denied by Server 2003 | Excel Programming | |||
Permission to use object denied | Excel Programming | |||
Permission Denied | Excel Programming |