如何让vba循环访问用户定义的文件夹,而不是手动选择每个文件夹

时间:2017-06-22 08:21:31

标签: vba outlook

Sub DeleteOld()

Dim oFolder As Folder
Dim dDate As Date
Dim ItemsOverDate As Outlook.Items
Dim dDays As Integer

Dim DateToCheck As String

dDays = InputBox("How many days?")

dDate = DateAdd("d", -dDays, Now())
dDate = Format(dDate, "dd/mm/yyyy")


Set oFolder = Application.Session.PickFolder 'or set your folder

DateToCheck = "[Received] <= """ & dDate & """"

Set ItemsOverDate = oFolder.Items.Restrict(DateToCheck)

For i = ItemsOverDate.Count To 1 Step -1
    ItemsOverDate.Item(i).Delete
Next


Set ItemsOverDate = Nothing
Set oFolder = Nothing


End Sub

2 个答案:

答案 0 :(得分:0)

我认为这就是你要找的东西。

Public FSO As New FileSystemObject

Private Sub DeleteOld()
MasterFolderDir = "LOCATION OF THE FOLDER THAT HOLDS ALL THE ONES YOU WANT TO DELETE"
iDate = InputBox("How many days?")
    For Each Folder In FSO.GetFolder(MasterFolderDir).SubFolders
        If DateDiff("d", Folder.DateCreated, Now) > iDate Then
            Folder.Delete
        End If
    Next
End Sub

让我知道它是否适合您。我没有测试它。

答案 1 :(得分:0)

您需要一个递归脚本来钻取文件夹,这些文件夹中的子文件夹以及这些子文件夹中的子文件夹。

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.contrib.postgres.search import SearchVector, SearchVectorField      
from django.db import migrations


def index_entries(apps, schema_editor):
    Entry = apps.get_model("mymodel", "Entry")
    Entry.objects.update(search_vector=SearchVector('body_text'))


class Migration(migrations.Migration):

    dependencies = [
        ('mymodel', '0001_initial'),
    ]

    operations = [
        migrations.AddField(
            model_name='entry',
            name='search_vector',
            field=SearchVectorField(null=True),
        ),

        migrations.RunPython(index_entries),

        migrations.AlterField(
            model_name='entry',
            name='search_vector',
            field=SearchVectorField(null=False),
        ),
    ]