Solved

VBA  Object does not support  property or method

Posted on 2016-10-20
3
28 Views
Last Modified: 2016-10-20
I copies this code from another thread by Bill Prew and modified it to work in Excel vba

it stops  at line  For Each objCheckFolder In objSubFolder.Folders  with error  Object does not support  property or method

any idea how to make this work and if there is any other issue to spot? thanks

Option Explicit



Sub macro()
    ' Local variables
    
Dim objFSO

Set objFSO = CreateObject("Scripting.FileSystemObject")
    Dim objFolder
    Dim objFile
    Dim objSubFolder
    Dim objCheckFile
    Dim objCheckFolder
    Dim Flag
    Dim strFolder As String
strFolder = "C:\Users\James\Downloads\Myfolder\"
    ' Acess the folder
    Set objFolder = objFSO.GetFolder(strFolder)

    ' Check each file in this folder
    For Each objFile In objFolder.Files

        ' If file is old enough delete it
        If DateDiff("h", objFile.DateCreated, Now()) >= 24 Then
            objFile.Delete True
        End If

    Next

    ' Now check all folders
    For Each objSubFolder In objFolder.Subfolders

        If DateDiff("h", objSubFolder.DateCreated, Now()) >= 24 Then
            ' Assume we can delete it
            Flag = "yes"

            ' Any new files that prevent deletion
            For Each objCheckFile In objSubFolder.Files
                If DateDiff("h", objCheckFile.DateCreated, Now()) < 24 Then
                    Flag = "no"
                End If
            Next

            ' Any new folders that prevent deletion
            For Each objCheckFolder In objSubFolder.Folders
                If DateDiff("h", objCheckFolder.DateCreated, Now()) < 24 Then
                    Flag = "no"
                End If
            Next

            ' Delete folder if no newer items found
            If Flag = "yes" Then
                objSubFolder.Delete True
            End If
        End If

    Next

End Sub

Open in new window

0
Comment
Question by:excelismagic
3 Comments
 
LVL 33

Expert Comment

by:Norie
ID: 41851638
I can't replicate the error, where are you running this code from?
0
 
LVL 48

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 41851648
Hi,

pls try
For Each objCheckFolder In objSubFolder.SubFolders

Open in new window

Regards
0
 
LVL 3

Author Closing Comment

by:excelismagic
ID: 41851827
thanks Rgonzo. after i replace the line with your code, it worked like a charm
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now