1

So, I read a lot of topics and similar questions, but could not understand properly.

I am running the code below, both the destinfilename and SourceFileName are correct, I checked with Debug.Print.

Debug gave me these results:

SourceFileName = "C:\Users\Renan\Desktop\BulkPdf\Documentos gerados\Doc.1-aaaaa.pdf"
namefile2 = "Doc.1-aaaaa.pdf"
destinfilename = "C:\Users\Renan\Desktop\BulkPdf\Documentos gerados\Doc.1-aaaaa"

When I copy the directory, and the destinfilename and copy on IE, work just perfect, but when running the code, always got "Error 53 File Not Found"

I tried putting and removing "", nothing worked.

NOTE 1: I want to move the file Doc.1-aaaaa.pdf to a folder with the same name, so the folder name is Doc.1-aaaaa

NOTE 2: I've been asking a lot of questions, I know and apologize, but I don't have a background on programming - I'm a lawyer - but I'm trying to learn something new by myself (without classes, just YouTube and forum) and VBA is really fun and useful.

I'm using this code. The problem is in the code?

Sub creating_pdfs()
Call LoopThroughFilesInFolder("C:\Users\Renan\Desktop\BulkPdf\Documentos gerados\", "Doc*")
End Sub


Sub LoopThroughFilesInFolder(strDir As String, Optional strType As String)
Dim FSO As Object
Dim SourceFileName As String, destinfilename As String
Dim namefile1 As String
Dim dot
Dim namefile2 As String


Dim file As Variant

If Right(strDir, 1) <> "\" Then strDir = strDir & "\"
file = Dir(strDir & strType)
While (file <> "")
Debug.Print file
SourceFileName = "C:\Users\Renan\Desktop\BulkPdf\Documentos gerados\" & file

namefile1 = file

dot = InStr(namefile1, "pdf") - 2

namefile2 = Left(namefile1, dot)



destinfilename = "C:\Users\Renan\Desktop\BulkPdf\Documentos gerados" & "\" & namefile2
Debug.Print namefile2
Debug.Print SourceFileName
Debug.Print destinfilename

Set FSO = CreateObject("scripting.FileSystemObject")

FSO.MoveFile Source = SourceFileName, Destination:=destinfilename



file = Dir

Wend


End Sub
Michael Wycisk
  • 1,590
  • 10
  • 24

1 Answers1

2

That code would not work the way it is because you have an evaluation in the source of your move. The evaluation of Source = SourceFileName is False. So you are trying to move a file named False, which doesn't exist.

Change this:

FSO.MoveFile Source = SourceFileName, Destination:=destinfilename

To this:

FSO.MoveFile SourceFileName, destinfilename

Other notes:

  • Call is deprecated, just remove it
  • while...wend is deprecated, use do while...loop or do...loop while instead.
  • Properly indent\space\format your code so it's easier to read.
  • Use FSO.GetFolder pattern instead of Dir
  • Use Option Explicit to avoid situations where an unintended new variable is created. In this case Source is a new variable that you didn't know you were using.
HackSlash
  • 4,944
  • 2
  • 18
  • 44
  • 1
    Just curious: Is there any downside to using `Call`? For me, it makes the code easier to read. – Michael Wycisk Jul 21 '20 at 23:09
  • If the procedure is a `Function` you would assign the return to a variable so `Call` would only ever be used for a `Sub`. The use of parenthesis changes if the parameters are passed `ByVal` or `ByRef`. This gets confusing with `Call` because it requires parenthesis. You should avoid passing `ByRef` if the procedure changes the value of what you passed in. This is called a "side-effect". You would use Functions instead of ByRef parameters. If you don't know what I am talking about then that is a whole subject to study. – HackSlash Jul 22 '20 at 15:25
  • 2
    `If the procedure is a Function you would assign the return to a variable so Call would only ever be used for a Sub` - not really, no. Many functions are called for their side effects. A good example is `MsgBox()` which is most often called as `MsgBox "message"`. `Call` is not deprecated in VBA and is a matter of style. You cannot accidentally end up with `ByRef` by using parentheses either, it's [the other way round](https://stackoverflow.com/a/8070104/11683). – GSerg Jul 22 '20 at 15:47
  • I wouldn't call the message display feature of MsgBox a side-effect because it doesn't change the state of anything in the program. It cannot modify flow. If you want to modify the flow of a program you have to capture the return of `MsgBox`, like any other function. Are you suggesting that you would `Call MsgBox("Message")` ? You even omitted `Call` in your example. – HackSlash Jul 22 '20 at 16:12