问题
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.
Alan
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:
Remove the parentheses:
MoveToArchiveFolder tempMailItem
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.
Here's an interesting Daily Dose post about why. Be sure to read the Rick Rothstein comment.
来源:https://stackoverflow.com/questions/13763955/object-required-error-in-outlook-vba