# Outlook 2007 - Run-Time error '438'



## f.nathan.walsh (Apr 11, 2011)

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


----------



## RoryA (Apr 11, 2011)

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.


----------



## f.nathan.walsh (Apr 11, 2011)

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


----------



## RoryA (Apr 11, 2011)

Yes - that was my suggestion.  Check the Class:

```
If MItem.Class = olMail Then
```

If you are late binding then add:

```
Const olMail as Long = 43
```


----------



## f.nathan.walsh (Apr 11, 2011)

Seems to have worked like a charm, thanks again

-Nathan


----------



## f.nathan.walsh (Apr 11, 2011)

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


----------



## RoryA (Apr 11, 2011)

You just need an Else part to the If MItem.Class test and then check the UnRead property as you did before.


----------



## f.nathan.walsh (Apr 11, 2011)

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


----------



## RoryA (Apr 11, 2011)

More like:

```
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
```


----------



## f.nathan.walsh (Apr 11, 2011)

perfect, thanks Rory

-Nathan


----------



## f.nathan.walsh (Apr 11, 2011)

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


----------



## f.nathan.walsh (Apr 11, 2011)

One final question.  Ever since I started using the above VBA code in Outlook, every time I unlock my computer (Windows XP) the VBA Editor opens to the code.  It does not error or anything, it is just annoying.  Has this ever happened before and does anyone know of a fix?

-Nathan


----------



## RoryA (Apr 11, 2011)

See this MSKB article.


----------



## f.nathan.walsh (Apr 11, 2011)

I will give that a go.  Thanks for the support.

-Nathan


----------

