0
 Dim FSO As Object
    Dim FromPath As String
    Dim ToPath As String
    Dim objFSO As Object
    Dim objFolder As Object
    Dim objSubFolder As Object
    Dim fil As File

    With Application.FileDialog(msoFileDialogFolderPicker) 'Choosing FromPath
        .Show
        FromPath = .SelectedItems(1) & "\"
    End With

    With Application.FileDialog(msoFileDialogFolderPicker) 'Choosing ToPath
        .Show
        ToPath = .SelectedItems(1) & "\"
    End With


   Set objFSO = CreateObject("Scripting.FileSystemObject")
   Set objFolder = objFSO.GetFolder(FromPath)
   For Each objSubFolder In objFolder.SubFolders
        **Set fils = fsoC.GetFolder(objSubFolder & "\").Files**

        For Each fil In fils
            If LCase(Right(fil.Name, 3)) = "zip" Then
               MsgBox "this is a zip file "
            Else

I'm getting object required error in the marked row , and I do not understand why ? Can anyone help me please ?

7
  • objSubFolder is an object, but you need a string, try fsoC.GetFolder(objSubFolder.PATH & "\").Files Sep 6, 2016 at 6:20
  • thank you, but it didn't help.
    – netanel
    Sep 6, 2016 at 6:27
  • fils seems to be not declared, maybe that would help (As Object or As Files) Sep 6, 2016 at 6:28
  • @RandySchuman Why do you think there is VBS too? Sep 6, 2016 at 6:29
  • fsoC is not declared.
    – DavidPostill
    Sep 6, 2016 at 15:22

1 Answer 1

0

fsoC seems to be not declared / instanciated in your script. Maybe you would like to use objFSO again ?

Edit :

Sub test()
 Dim fils As Object
 Dim fil As Object
 Dim FromPath As String
 Dim ToPath As String
 Dim objFSO As Object
 Dim objFolder As Object
 Dim objSubFolder As Object

 With Application.FileDialog(msoFileDialogFolderPicker) 'Choosing FromPath
     .Show
     FromPath = .SelectedItems(1) & "\"
 End With

 With Application.FileDialog(msoFileDialogFolderPicker) 'Choosing ToPath
     .Show
     ToPath = .SelectedItems(1) & "\"
 End With


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(FromPath)
For Each objSubFolder In objFolder.SubFolders
   Set fils = objFSO.GetFolder(objSubFolder & "\").Files

   For Each fil In fils
      If LCase(Right(fil.Name, 3)) = "zip" Then
         MsgBox "this is a zip file "
      Else
         MsgBox "NOPE"
      End If
    Next
Next
End Sub

This code works with the following folder :

A
-- A.1
---- test.txt
---- test.zip
4
  • Thanks , I forgot to set a variable at the beginning of the code you're right
    – netanel
    Sep 6, 2016 at 6:31
  • Why do you declare ObjFSO and FSO but only use one of them? Sep 6, 2016 at 7:13
  • I just update the object used at the marked line and add fils declaration. It was more of a copy/paste example than an clear code of the author. I'll edit it. Thanks !
    – Kaël
    Sep 6, 2016 at 7:49
  • @netanel if this answered your question, please select the answer.
    – David S
    Sep 23, 2021 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.