Need help to fix Dir error (Run-Time error 5) - Invalid Procedure or argument call

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
I have tried to add some pieces of codes together to produce this piece of algorithm for myself. When I run it and it does not kill nor move any file, then it reports the error message, Run-Time error 5 invalid procedure or argument call then it highlights the line:


Code:
OriginalFile = Dir


The renaming becomes successful. Just that that error prevents the other codes from running. I don’t know if it is best to use the “resume next” statement to force it to run the other codes or not.


I need some deep mind to point out my mistakes for me. Thanks


Code:
    Dim fPath$, oPath$, Ext$, fso As Object
    Dim OriginalFile$, FileExists$, OffExists$
    Set fso = CreateObject("Scripting.FileSystemObject")
    With ThisWorkbook
        fPath = .Path & "\PASSPORT PICTURES\"
        oPath = .Path & "\PASSPORT OFFLINE\"
    End With
    OriginalFile = Dir(fPath & ImageEnter & ".*")
    FileExists = Dir(fPath & ImageChange & ".*")
    OffExists = Dir(oPath & ImageChange & ".*")
    If Len(ImageChange) Then
        If Len(FileExists) Then
            If Len(OffExists) Then Kill oPath & ImageChange & ".*"
            fso.movefile Source:=fPath & ImageChange & ".*", Destination:=oPath
        End If
        While Len(OriginalFile) > 0
            Ext = Right(OriginalFile, Len(OriginalFile) + 1 - InStrRev(OriginalFile, "."))
            Name fPath & ImageEnter & Ext As fPath & ImageChange & Ext
            OriginalFile = Dir
        Wend
    End If

Note

The variables ImageEnter and ImageChange are global variables that are assigned in a different procedure - in case you wanna know where they are set or defined.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Here is the way x = Dir works. After the last Dir, it uses the input from it. Your last Dir before the one in the loop is:
Code:
OffExists = Dir(oPath & ImageChange & ".*")
Move this line to before the first If to fix it.
Code:
    OriginalFile = Dir(fPath & ImageEnter & ".*")
Some use Len()<>0 to check for fileexists using Dir(). I do this:
Code:
FileExists = Dir(fPath & ImageChange & ".*")<>""
This returns a boolean value, True or False.

I would recommend using all VBA or fso command methods. This will avoid some issues. You can mix the two methods but watch out for the Dir() issue as I detailed.
 
Last edited:
Upvote 0
Here is the way x = Dir works. After the last Dir, it uses the input from it. Your last Dir before the one in the loop is:
Code:
OffExists = Dir(oPath & ImageChange & ".*")
Move this line to before the first If to fix it.
Code:
    OriginalFile = Dir(fPath & ImageEnter & ".*")
Some use Len()<>0 to check for fileexists using Dir(). I do this:
Code:
FileExists = Dir(fPath & ImageChange & ".*")<>""
This returns a boolean value, True or False.

I would recommend using all VBA or fso command methods. This will avoid some issues. You can mix the two methods but watch out for the Dir() issue as I detailed.

Code:
   OffExists = Dir(oPath & ImageChange & ".*")
   OriginalFile = Dir(fPath & ImageEnter & ".*")
    FileExists = Dir(fPath & ImageChange & ".*")

This is how I understood what you said. But it didn't work. Can you confirm I did the right thing?

If I did the opposite the please fill me in with more details. :)
 
Upvote 0
No.
Code:
OffExists = Dir(oPath & ImageChange & ".*")<>""
 FileExists = Dir(fPath & ImageChange & ".*")<>""
 OriginalFile = Dir(fPath & ImageEnter & ".*")

FSO also has a way to check for FileExists, iterate files in a folder, get fileExtention, and more. If you don't have the help file for it, you can get it from me. Microsoft removed their link a long time ago. You may have to right click it and change Properties to unlock it since it is a CHM help file. https://www.dropbox.com/s/03lqmrsdr83a0z2/script56.chm?dl=1

If still stuck, post back which method you prefer.
 
Upvote 0
No.
Code:
OffExists = Dir(oPath & ImageChange & ".*")<>""
 FileExists = Dir(fPath & ImageChange & ".*")<>""
 OriginalFile = Dir(fPath & ImageEnter & ".*")

FSO also has a way to check for FileExists, iterate files in a folder, get fileExtention, and more. If you don't have the help file for it, you can get it from me. Microsoft removed their link a long time ago. You may have to right click it and change Properties to unlock it since it is a CHM help file. https://www.dropbox.com/s/03lqmrsdr83a0z2/script56.chm?dl=1

If still stuck, post back which method you prefer.

After doing that the codes reports an error " Run-Time error 53" - "File Not Found" on the line

Code:
If Len(OffExists) Then Kill oPath & ImageChange & ".*"

While it highlights

Code:
Kill oPath & ImageChange & ".*"

By then, the " OffExists" has a value = false which makes that Len function true but when it enters the folder to kill the file, there was no file.

The "FileExists" also has same false value at the time.

Since the code did not run to the bottom, I can not determine if that's all the possible traps.



As to the method I prefer, I will go for any that works for me.

I am still going through the file, but I have not yet acquired the skills to fix my problem yet. :LOL:

Regards
 
Upvote 0
Okay this version of the code is working for all instances.

Whether I kill a file, or move a file

Code:
 OffExists = Dir(oPath & ImageChange & ".*")
 FileExists = Dir(fPath & ImageChange & ".*")
 OriginalFile = Dir(fPath & ImageEnter & ".*")

I am very grateful. I hope that's neat right now and that all my other lines are okay?

If there is a problem somewhere I would be glad someone can point out it for me for future issues sake.

Since I don't really understand this Dir manipulation that much
 
Upvote 0

Forum statistics

Threads
1,214,391
Messages
6,119,244
Members
448,879
Latest member
VanGirl

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top