根据日期和类别

时间:2016-01-19 01:26:35

标签: vba outlook outlook-vba

为什么以下VBA脚本不会根据日期类别移动项目?它将超过七天的项目移动到名为的不同文件夹,但是它正在移动的项目似乎是随机的,AFG类别中的不是项目。

Sub MoveAgedMail()

Dim objOutlook As Outlook.Application
Dim objNamespace As Outlook.NameSpace
Dim objSourceFolder As Outlook.MAPIFolder
Dim objDestFolder As Outlook.MAPIFolder
Dim objVariant As Variant
Dim lngMovedItems As Long
Dim intCount As Integer
Dim intDateDiff As Integer
Dim strDestFolder As String

Set objOutlook = Application
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objSourceFolder = objNamespace.GetDefaultFolder(olFolderInbox)

' use a subfolder under Inbox
Set objDestFolder = objSourceFolder.Folders("Old")

For intCount = objSourceFolder.Items.Count To 1 Step -1
    Set objVariant = objSourceFolder.Items.Item(intCount)
    DoEvents
    If objVariant.Class = olMail Then


         intDateDiff = DateDiff("d", objVariant.SentOn, Now)

        ' I'm using 7 days, adjust as needed.
        If intDateDiff > 7 And objVariant.Categories = AFG Then

          objVariant.Move objDestFolder
          ' MsgBox intDateDiff
          'count the # of items moved
           lngMovedItems = lngMovedItems + 1

        End If
    End If
Next

' Display the number of items that were moved.
MsgBox "Moved " & lngMovedItems & " messages(s)."
 Set objDestFolder = Nothing
 End Sub

编辑 - 抱歉,我的代码正在进行中。 如果太难理解我可以尝试清理它!

1 个答案:

答案 0 :(得分:0)

我没有在AFG周围加注。

 If intDateDiff > 7 And objVariant.Categories = AFG Then

成为

 If intDateDiff > 7 And objVariant.Categories = "AFG" Then

我的坏。