本文介绍了当收件箱中的项目声明为邮件项时,键入不匹配错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Outlook中具有以下VBA代码,可以将邮件移动到旧的个人文件夹中.这是代码:

I have the following VBA code in Outlook to move mail to a personal folder if it is old. Here is the code:

我在Next objItem行上看到一个异常(看着它没有设置).

I get an exception on the line Next objItem (looking at the watch it is set to nothing).

什么会导致objItem为null,从而导致下一个objItem 行中的类型不匹配异常?

What would cause objItem to be null and thus cause a Type Mismatch exception in the Next objItem line?

Sub MoveOldMailFromInbox()

Dim objFolder As Outlook.MAPIFolder
Dim objNS As Outlook.NameSpace, objItem As Outlook.MailItem, mail As Outlook.MailItem

Set objNS = Application.GetNamespace("MAPI")

Dim Inbox As MAPIFolder
Set Inbox = objNS.GetDefaultFolder(olFolderInbox)

Dim mailToMove As New Collection

Dim EightyFiveDaysAgo As Date
EightyFiveDaysAgo = DateAdd("d", -85, Date)

Set objFolder = objNS.Folders("PersonalFolders").Folders("InboxOlderThan85Days")
If objFolder Is Nothing Then
    MsgBox "This folder doesn't exist!", vbOKOnly + vbExclamation, "INVALID FOLDER"
End If

For Each objItem In Inbox.Items
    If objFolder.DefaultItemType = olMailItem Then

        If objItem.Class = olMail And objItem.ReceivedTime < EightyFiveDaysAgo Then

            mailToMove.Add objItem

        End If
    End If
Next objItem


For Each mail In mailToMove
    mail.UnRead = False
    mail.Move objFolder
Next mail

Set objItem = Nothing
Set objFolder = Nothing
Set objNS = Nothing

End Sub

推荐答案

您正在遍历Inbox.Items,但是变量objItem被定义为MailItem-收件箱中的项目可能并不总是MailItem.

You're iterating through Inbox.Items but your variable objItem is defined as MailItem - an item in your inbox might not always be a MailItem.

尝试

Dim objItem as Object

这篇关于当收件箱中的项目声明为邮件项时,键入不匹配错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-22 02:48