Runtime error 438 - Outlook

mrshl9898

Well-known Member
Joined
Feb 6, 2012
Messages
1,951
Good morning/afternoon/evening,

I'm trying to pull in some email data and have the below error on the Debug line.

I have the Outlook 16.0 Reference Library added.

Any suggestions?

Many thanks.

1620253974919.png




VBA Code:
Sub GetFromOutlook()

Dim myOlApp As New Outlook.Application
Dim myNameSpace As Outlook.Namespace
Dim myInbox As Outlook.MAPIFolder
Dim myitems As Outlook.Items
Dim myitem As Object
Dim i As Integer
Dim myDate As Date
Dim sht As Worksheet

Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myitems = myInbox.Items
Set sht = Sheets("Data")
myDate = Date
i = 1


For Each myitem In myitems
Debug.Print myitem.ReceivedTime
    If myitem.ReceivedTime >= myDate And InStr(1, myitem.Subject, "Town") > 0 Then
        sht.Cells(i + 1, 1) = myitem.Subject
        sht.Cells(i + 1, 2) = myitem.ReceivedTime
        sht.Cells(i + 1, 3) = myitem.SenderName
        sht.Cells(i + 1, 4) = myitem.Body
        i = i + 1
    End If
Next myitem

End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
There is more than one type of item. You can get the one you want by either method:
VBA Code:
Debug.Print TypeName(myItem)="MailItem", myItem.Class=olMail
If either is true, then your code should "work".
 
Upvote 0
Solution
Many thanks for the reply. I still seem to be missing something though.

Both were True

Error now occurring on:

VBA Code:
If myitem.ReceivedTime >= myDate And myitem.Subject = "Export Issues" Then


VBA Code:
Sub GetFromOutlook()

Dim myOlApp As New Outlook.Application
Dim myNameSpace As Outlook.Namespace
Dim myInbox As Outlook.MAPIFolder
Dim myitems As Outlook.Items
''Dim myitem As Object
Dim i As Integer
Dim myDate As Date
Dim sht As Worksheet

Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myitems = myInbox.Items
Set sht = Sheets("Data")
myDate = Date
i = 2


For Each myitem In myitems
If myitem.Class = olMail Then
    If myitem.ReceivedTime >= myDate And myitem.Subject = "Export Issues" Then
        sht.Cells(i + 1, 1) = myitem.Subject
        sht.Cells(i + 1, 2) = myitem.ReceivedTime
        sht.Cells(i + 1, 3) = OutlookMail.SenderName
        sht.Cells(i + 1, 4) = myitem.Body
        i = i + 1
    End If
    If myitem.ReceivedTime >= myDate And myitem.Subject = "Import Issues" Then
        sht.Cells(i + 1, 1) = myitem.Subject
        sht.Cells(i + 1, 2) = myitem.ReceivedTime
        sht.Cells(i + 1, 3) = OutlookMail.SenderName
        sht.Cells(i + 1, 4) = myitem.Body
        i = i + 1
    End If
End If
Next myitem

End Sub
 
Upvote 0
EDIT: Looks like ReceivedTime isn't valid.

Is there a way to only search todays emails? Ideally not searching the whole inbox
 
Upvote 0
I recommend that users always use Option Explicit as first line in a module and Debug > Compile before a run. As such, you should uncomment the myitem Dim line and change the two "OutlookMail." lines to myitem...

I had no error in my run.
 
Upvote 0
This seems to work for me.
VBA Code:
If myitem.ReceivedTime >= myDate Then Debug.Print myitem.Subject
Maybe your subject line check is the issue for not listing a mailitem?
 
Upvote 0
This is fine, looks to be ReceivedTime was the issue.

I'll get the team to move it to a seperate folder and keep it clean to avoid excessive running times.

Many thanks for the help.

VBA Code:
Sub GetFromOutlook()

Dim myOlApp As New Outlook.Application
Dim myNameSpace As Outlook.Namespace
Dim myInbox As Outlook.MAPIFolder
Dim myitems As Outlook.Items
Dim myitem As Object
Dim i As Integer
Dim myDate As Date
Dim sht As Worksheet

Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox).Folders("Owens Test")
Set myitems = myInbox.Items
Set sht = Sheets("Data")
myDate = Date
i = 2


For Each myitem In myitems
If myitem.Class = olMail Then
Debug.Print myitem.Class
Debug.Print myitem.Subject
    If myitem.Subject = "Export Issues" Then
        sht.Cells(i + 1, 2) = myitem.ReceivedTime
        sht.Cells(i + 1, 4) = myitem.Body
        i = i + 1
    End If
    If myitem.Subject = "Import Issues" Then
        sht.Cells(i + 1, 2) = myitem.ReceivedTime
        sht.Cells(i + 1, 3) = myitem.SenderName
        i = i + 1
    End If
End If
Next myitem

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,039
Messages
6,122,802
Members
449,095
Latest member
m_smith_solihull

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