Outlook 2007 - Run-Time error '438'

f.nathan.walsh

New Member
Joined
Feb 1, 2010
Messages
9
Hello everyone, I am getting a Run-time error '438' : Object doesn't support this property or method in the code below on this line: sentDate = Format(MItem.SentOn, "mm/dd/yyyy"). Any Ideas?

-Nathan

Sub MoveEmail()
Dim olMAPI As Object 'Outlook.Application
Dim moveFolder As Object 'Outlook.MAPIFolder
Dim InItem As Object 'Outlook.MAPIFolder
Dim MItem As Object 'Outlook.MailItem
Dim sentDate As Date
Dim sentDate2 As Date
Dim myDay As Integer
Dim i As Integer
Dim t As Date
t = Now()

Set olMAPI = GetObject("", "Outlook.Application").GetNamespace("MAPI")
Set InItem = olMAPI.Folders("Mailbox - Walsh, Nathan").Folders("inbox")
Set moveFolder = olMAPI.Folders("F2011").Folders("Inbox")
i = 0
Count = InItem.Items.Count
For i = Count To 1 Step -1
Set MItem = InItem.Items.Item(i)

mySub = MItem.Subject
sentDate = Format(MItem.SentOn, "mm/dd/yyyy")
myDay = Date - sentDate
If myDay >= 14 Then

If MItem.UnRead = False Then
MItem.Move moveFolder
End If
End If

Next
Set moveFolder = Nothing
Set dltFolder = Nothing
Set InItem = Nothing
Set MItem = Nothing
MsgBox "the macro finished in " & Format(Now() - t, "hh:mm:ss")

End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Not all items have a SentOn property. I suspect you have something in your Inbox that is not a MailItem so you should check the Class of the item before you try and manipulate it.
 
Upvote 0
Thanks for the quick reply Rory. There was item that was not mail and that corrected the problem. Would it be possilbe to use an "if then" statement to check if it is a Mail item and skip the item if it is not?

-Nathan
 
Upvote 0
Yes - that was my suggestion. ;) Check the Class:
Code:
If MItem.Class = olMail Then

If you are late binding then add:
Code:
Const olMail as Long = 43
 
Upvote 0
After thinking about this for a bit, what I would like is for any items that are not mail to be moved to the .pst folder regardless of the date, but as long as they are read. So to try to sum it up:

Mail items
if read and over 14 days since sent date then move to .pst

Non-mail items
if read move to .pst

here is the code as of now, any help is greatly appreciated

-Nathan

Sub MoveEmail()
Dim olMAPI As Object 'Outlook.Application
Dim moveFolder As Object 'Outlook.MAPIFolder
Dim InItem As Object 'Outlook.MAPIFolder
Dim MItem As Object 'Outlook.MailItem
Dim sentDate As Date
Dim sentDate2 As Date
Dim myDay As Integer
Dim i As Integer
Dim t As Date
t = Now()

Set olMAPI = GetObject("", "Outlook.Application").GetNamespace("MAPI")
Set InItem = olMAPI.Folders("Mailbox - Walsh, Nathan").Folders("inbox")
Set moveFolder = olMAPI.Folders("F2011").Folders("Inbox")
i = 0
Count = InItem.Items.Count
For i = Count To 1 Step -1
Set MItem = InItem.Items.Item(i)

If MItem.Class = olMail Then

mySub = MItem.Subject
sentDate = Format(MItem.SentOn, "mm/dd/yyyy")
myDay = Date - sentDate
If myDay >= 14 Then

If MItem.UnRead = False Then
MItem.Move moveFolder
End If
End If
End If

Next
Set moveFolder = Nothing
Set dltFolder = Nothing
Set InItem = Nothing
Set MItem = Nothing
MsgBox "the macro finished in " & Format(Now() - t, "hh:mm:ss")

End Sub
 
Upvote 0
You just need an Else part to the If MItem.Class test and then check the UnRead property as you did before.
 
Upvote 0
So would it look like this?

Set MItem = InItem.Items.Item(i)

If MItem.Class = olMail Then
Else
If MItem.UnRead = False Then
MItem.Move moveFolder

mySub = MItem.Subject
sentDate = Format(MItem.SentOn, "mm/dd/yyyy")
myDay = Date - sentDate
If myDay >= 14 Then

If MItem.UnRead = False Then
MItem.Move moveFolder


Sorry, I am quite new to this.

-Nathan
 
Upvote 0
More like:
Code:
Set MItem = InItem.Items.Item(i)

If MItem.Class = olMail Then

   mySub = MItem.Subject
   sentDate = Format(MItem.SentOn, "mm/dd/yyyy") 
   myDay = Date - sentDate
   If myDay >= 14 Then

      If MItem.UnRead = False Then
         MItem.Move moveFolder
      End If
   End If
Else
   If MItem.UnRead = False Then
      MItem.Move moveFolder
   End If
End If
 
Upvote 0

Forum statistics

Threads
1,224,585
Messages
6,179,706
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top