Outlook vba中的对象需要错误
我收到此 Object Required 错误.请参阅下面的代码.我不明白为什么我会得到它.oitem 变量被声明为一个对象,我在尝试移动它之前对MailItem"类型进行了测试.请指教.
I'm getting this Object Required error. See code below. I don't understand why I'm getting it. The oitem variable is declared as an Object, and I do a test for the type "MailItem" before attempting to move it. Please advise.
艾伦
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
去掉括号:
MoveToArchiveFolder tempMailItem
基本上括号表示要评估 tempMailItem
的值.MailItem
的默认属性是电子邮件的主题,因此您的代码将 tempMailItem
的主题传递给您的函数,而不是 MailItem
本身.
Basically the parentheses say to evaluate the value of tempMailItem
. The default property of a MailItem
is the email's subject, so your code is passing tempMailItem
's subject to your function, instead of MailItem
itself.
这是一个有趣的每日剂量发帖说明原因.请务必阅读 Rick Rothstein 的评论.
Here's an interesting Daily Dose post about why. Be sure to read the Rick Rothstein comment.