asked on
Hello Team
Until recently the following code has been executing without any issues.
Sub MoveFiles()
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
Dim FileExt As String
Dim fNames As String
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:= _
"K:\Bus\Plan\Daily MailBox Count\MailCount Peer.xlsm", FileFormat:= _
xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
Application.DisplayAlerts = True
FromPath = "K:\Bus\Plan\Daily MailBox Count\"
ToPath = "K:\Bus\Plan\Daily MailBox Count\Current Year Mail Count Archive\"
FileExt = "*Mailbox Count Report*"
fNames = Dir(FromPath & FileExt)
If Len(fNames) = 0 Then
MsgBox "No files in " & FromPath
Exit Sub
End If
Set FSO = CreateObject("scripting.filesystemobject")
FSO.MoveFile Source:=FromPath & FileExt, Destination:=ToPath
' MsgBox "You can find the files from " & FromPath & " in " & ToPath
End Sub
Now for some reason the following code line produces a runtime error 70 'Permission Denied'
FSO.MoveFile Source:=FromPath & FileExt, Destination:=ToPath
The referenced path and folders are located on a network and I continue to have full permission to access and work within the referenced path and folders.
What I have discovered is that even though when the FSO.MoveFile code line is producing the runtime error, before it displays the error the file is actually moved to the destination path and folder.
Can anyone please shed some light on why the the file would be successfully moved but then be followed up with a 'Permission Denied' runtime error ?
Thank you in advance.
Sincerely
Dale
fNames = Dir(strFolder & "*~*", vbHidden + vbSystem)
static cntr as long
cntr = cntr + 1
debug.print "cntr= " & cntr & " first file= <" & fnames & ">"
fso.MoveFile Source:=FromPath & fileext, Destination:=ToPath '
fNames = Dir(FromPath & fileext, vbHidden + vbSystem)
If fNames = "" Then
MsgBox "Case 1: All files were moved, and err = " & Err
Else
MsgBox "Case 2: Some files were not moved, so err = " & Err
End If
On Error Resume Next
fso.MoveFile Source:=FromPath & fileext, Destination:=ToPath
if err <> 70 and err <> 0 then
msgbox err & error
end if
On Error GoTo 0
Sub MoveBugs()
Const FromPath = "c:\Bus\Plan\Daily MailBox Count\"
Const ToPath = "c:\Bus\Plan\Daily MailBox Count\Current Year Mail Count Archive\"
Const fileext = "*Mailbox Count Report*"
Dim fNames
Dim fso As Object
Set fso = CreateObject("scripting.filesystemobject")
fso.MoveFile Source:=FromPath & fileext, Destination:=ToPath '
fNames = Dir(FromPath & fileext, vbHidden + vbSystem)
If fNames = "" Then
MsgBox "All files were moved, and err = " & Err
Else
MsgBox "Some files were not moved, so err = " & Err
End If
End Sub
ASKER
ASKER
ASKER
ASKER
Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.
TRUSTED BY
You need to clean up your method and add proper error handling. Especially as you've multiple sources for that error, namely the Dir() and FSO. E.g.
Open in new window
I would expect the Dir() to fail due to the use of a mapped drive and some saved credential issues with a delayed network reaction.