Run-Time error '424' Object required.

Lochwood

Registered User.
Local time
Today, 03:20
Joined
Jun 7, 2017
Messages
130
I am trying to delete empty subfolders from Doc-Production older using a button but am getting the following error. Run-Time error '424' Object required. Code stops at imoji.

Private Sub Command65_Click()

'On Error Resume Next ' <-- Comment out for troubleshooting.

Const Prep = "\\servername\DART$\Document-Library\Doc-Production"

Dim objFolder, objSubFolder, objFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.getFolder(Prep)

Call Search(objFolder)
End Sub
Sub Search(objFolder)


:banghead:For Each objSubFolder In objFolder.SubFolders
Search (objSubFolder)
Next
For Each objSubFolder In objFolder
If objSubFolder.Files.Count = 0 Then objSubFolder.Delete
Next
End Sub
 
i use:
Set FileSystem = CreateObject("Scripting.FileSystemObject")
Search FileSystem.GetFolder(HostFolder)

and it works.
 
getting the same error which makes me think the code is in the wrong place.
 

Attachments

Last edited:
You've DIM'd objSubFolder in the click routine, but not in the second. The object is therefore out of scope in your Search sub.
 
Cheers Cronk but getting same error.
 

Attachments

  • Capture4 .JPG
    Capture4 .JPG
    61.2 KB · Views: 187
Then maybe your Search routine is no good. Try

Code:
Sub Search(objFolder)
   dim objsubfolder
   
   if objfolder.files.count < 1 Then
      objfolder.delete
      exit sub
   endif
   
   if objfolder.subFolders.count > 0 Then
      for each objsubfolder in objfolder.subFolders
         call search(objsubfolder)
      next
   endif

End Sub
 
You sent me on the right track.. Thanks. Here's the code that worked for others.

Code:
Private Sub Command65_Click()
On Error Resume Next ' <-- Comment out for troubleshooting.

Const Prep = "\\Servername\DART$\Document-Library\Doc-Production"

Dim objfolder
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objfolder = objFSO.getFolder(Prep)

Call Search(objfolder)
End Sub
Sub Search(objfolder)

Const Prep = "\\Servername\DART$\Document-Library\Doc-Production"
Dim objsubfolder, objfile

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objfolder = objFSO.getFolder(Prep)

   'For Each objfile In objfolder.Files
        'If DateDiff("d", objfile.dateLastModified, Now) > 30 Then objfile.Delete
    'Next
    
    For Each objsubfolder In objfolder.SubFolders
    If objsubfolder.Files.Count = 0 Then objsubfolder.Delete
    Next
        
End Sub
 
Last edited by a moderator:
Sub Search(objFolder as Object)



Sent from my HTC One A9s using Tapatalk
 

Users who are viewing this thread

Back
Top Bottom