outlook vba中对象所需的错误

时间:2012-12-07 13:21:44

标签: vba outlook-vba

我收到此Object Required错误。见下面的代码。我不明白为什么我得到它。 oitem变量被声明为Object,我在尝试移动之前对“MailItem”类型进行了测试。请指教。

艾伦

Public StatsArchiveFolder As Outlook.Folder
'StatsArchiveFolder is set to equal an Outlook.Folder elsewhere
_____________________________________

Sub MoveHarpStatMail()

Dim olapp As Outlook.Application
Dim olappns As Outlook.NameSpace
Dim oitem As Object
Dim ItemsToProcess As Outlook.Items
Dim myFolder As MAPIFolder
Dim sFilter As String
Dim tempMailItem As Outlook.MailItem

On Error GoTo LocalErr

'set outlook objects

Set olapp = New Outlook.Application
Set olappns = olapp.GetNamespace("MAPI")
Set myFolder = olappns.GetDefaultFolder(olFolderInbox)
'Filter or only MailItems received today
sFilter = "[ReceivedTime] >= " & AddQuotes(Format(Date, "ddddd"))
Set ItemsToProcess = Session.GetDefaultFolder(olFolderInbox).Items.Restrict(sFilter)

For Each oitem In ItemsToProcess
 If TypeName(oitem) = "MailItem" Then
   Set tempMailItem = oitem
   If CheckSubject(tempMailItem.Subject) Then
     MoveToArchiveFolder (tempMailItem) '<<<Error 424 Object Required ???
   End If
 End If
Next oitem

ExitProc:
Set olapp = Nothing
Set olappns = Nothing
Set myFolder = Nothing
Set ItemsToProcess = Nothing

...
End Sub

_________________________________

Function CheckSubject(Subject As String) As Boolean

   If (LCase(Trim(Subject)) = LCase(SubjectTitle)) Then
     CheckSubject = True
   Else
     CheckSubject = False
   End If

 End Function

______________________________

Sub MoveToArchiveFolder(Item As Outlook.MailItem)

  If (ArchiveFolder = Nothing) Then
    MsgBox ("The ArchiveFolder object is not set.")
  End If

  Item.Move (StatsArchiveFolder)

End Sub

1 个答案:

答案 0 :(得分:5)

删除括号:

MoveToArchiveFolder tempMailItem

基本上括号表示要评估tempMailItem的值。 MailItem的默认属性是电子邮件的主题,因此您的代码会根据您的函数传递tempMailItem,而不是MailItem本身。

这是一篇有趣的Daily Dose帖子,内容涉及原因。一定要阅读Rick Rothstein的评论。