3

我收到此 Object Required 错误。请参阅下面的代码。我不明白为什么我会得到它。oitem 变量被声明为一个对象,我在尝试移动它之前对“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
4

1 回答 1

5

去掉括号:

MoveToArchiveFolder tempMailItem

基本上括号说要评估 的值tempMailItem。a 的默认属性MailItem是电子邮件的主题,因此您的代码将tempMailItem' 主题传递给您的函数,而不是MailItem它本身。

这是一篇关于原因的有趣的每日剂量帖子。请务必阅读 Rick Rothstein 的评论。

于 2012-12-07T14:39:35.757 回答