PDA

View Full Version : [SOLVED] Run-time error 53 when moving files



mtodorovski
11-16-2017, 07:23 AM
Hello All, i am trying to move newly created files (invoice files) into also newly created folder for each.
I am creating a .xlsx file and .pdf file, and want to move them both in the new folder.
But i get run-time error 53 when doing so, if i get rid of the xlsx file, moves the pdf file with no problem, once i tell it to move the xlsx file, it gives me the error, but the file is present.
How can i fix that, here is my code:


Sub SaveAsPDF()
'SaveAsPDF-Inv Macro
' Save a pdf of file and name it
If Len(Dir("C:\Users\mtodorovski\Dropbox\INVOICES\INVOICE#00" & Range("F5").Value & Property, vbDirectory)) = 0 Then
MkDir "C:\Users\mtodorovski\Dropbox\INVOICES\INVOICE#00" & Range("F5").Value & Property
End If
ChDir "C:\Users\mtodorovski\Dropbox\INVOICES"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:="C:\Users\mtodorovski\Dropbox\INVOICES\" & "00" & Range("F5").Value
Dim NewFN As Variant
ActiveSheet.Copy
NewFN = "C:\Users\mtodorovski\Dropbox\INVOICES\INVOICE#00" & Range("F5").Value
ActiveWorkbook.SaveAs NewFN, FileFormat:=xlOpenXMLWorkbook
ActiveWorkbook.Close
Application.Wait (Now + TimeValue("0:00:01"))
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
Dim FileExt As String
Dim FileName As String
FromPath = "C:\Users\mtodorovski\Dropbox\INVOICES" '<< Change
ToPath = "C:\Users\mtodorovski\Dropbox\INVOICES\INVOICE#00" & Range("F5").Value '<< Change
FileExt = "*.pdf"
FileName = "INVOICE#00" & Range("F5").Value '<< Change
'You can use *.* for all files or *.doc for Word files
If Right(FromPath, 1) <> "\" Then
FromPath = FromPath & "\"
End If
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(FromPath) = False Then
MsgBox FromPath & " doesn't exist"
Exit Sub
End If
If FSO.FolderExists(ToPath) = False Then
MsgBox ToPath & " doesn't exist"
Exit Sub
End If
Application.Wait (Now + TimeValue("0:00:04"))
FSO.MoveFile Source:=FromPath & FileExt & FileName, Destination:=ToPath
End Sub

Kenneth Hobs
11-16-2017, 03:22 PM
Welcome to the forum!

Check your order and missing backslash character. Press Ctrl+G after a run to view the result below.

Debug.Print FromPath & FileExt & Filename
'FSO.MoveFile Source:=FromPath & FileExt & FileName, Destination:=ToPath

mtodorovski
11-20-2017, 07:09 AM
Welcome to the forum!

Check your order and missing backslash character. Press Ctrl+G after a run to view the result below.

Debug.Print FromPath & FileExt & Filename
'FSO.MoveFile Source:=FromPath & FileExt & FileName, Destination:=ToPath

THANKS KEN, I JUST DOUBLED THE FILE EXT AND TO PATH BELOW EVERYTHING AND IT WORKED, MACRO DOES WHAT I EXPECT IT SO I LEFT IT ALONE HAHA.

THANKS FOR YOUR KIND REPLY