0

I was running a Windows Task Scheduler task every morning to run a macro in an Excel file. My task didn't work because the VBA code is now giving me an error. The VBA code was 100% functional before today.

I get

"Object doesn't support this property or method"

Dim olApp As Object
Dim olNS As Object
Dim myDate As Date
Dim olItems As Object
Dim olItem As Object
Dim olAttach As Object
Dim Date1 As String
Dim Date2 As String
Dim iAttachments As Integer

Date1 = Date & " " & TimeValue("6:00:00")
Date2 = Date & " " & TimeValue("00:00:00")

On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
Err.Clear: On Error GoTo 0

If olApp Is Nothing Then
Set olApp = CreateObject("Outlook.Application")
End If

Set olNS = olApp.GetNamespace("MAPI")

Set olItems = olNS.GetDefaultFolder(olFolderInbox).Items

For Each olItem In olItems

    If olItem.ReceivedTime < Date1 Then  '<----- ERROR LINE
        If olItem.ReceivedTime > Date2 Then
            If InStr(olItem.Body, "Darth Vader") > 0 Then

                iAttachments = olItem.Attachments.Count + iAttachments

                Set olAttach = olItem.Attachments.Item(1)

                On Error GoTo Err_Handler
                olAttach.SaveAsFile "C:\Desktop\Automatic Outlook Downloads" & "\" & olAttach.Filename

                Set olAttach = Nothing
                Set olItem = Nothing

                If iAttachments = 4 Then Exit For
            End If
        End If
    End If
Next

    Set olAttach = Nothing
    Set olItem = Nothing
    Set olApp = Nothing
    Set olNS = Nothing
    Set olItems = Nothing

Exit Sub
Community
  • 1
  • 1
Computer_Nerd
  • 102
  • 11
  • 1
    This must mean that some of the items in your inbox do not have a `ReceivedTime` property. Which [*type* of items](https://learn.microsoft.com/en-us/office/vba/api/outlook.olitemtype) are you concerned with? Presumably `MailItems` (which will all have this property). Are there any other types you care about? – David Zemens May 09 '19 at 18:28
  • I'm just looking to extract 4 attachments from my inbox that are received via email everyday between the hours of 12:00 AM and 6:00 AM. So no, I don't care about other types. – Computer_Nerd May 09 '19 at 18:32
  • 2
    You need to make sure `olItem` is a `MailItem`. Just verify that `TypeName(olItem) = "MailItem"` before you try accessing its members, should be good. – Mathieu Guindon May 09 '19 at 18:46

2 Answers2

4

Some Items in the Inbox may not be MailItems or otherwise may not have a ReceivedTime property. Since you are only concerned with the MailItem type, you should be able to use the following conditional check within your For Each:

For Each olItem In olItems
    'With early binding, you could use:
    ' If TypeOf olItem Is MailItem Then 
    'Otherwise:
    If TypeName(olItem) = "MailItem" Then
        If olItem.ReceivedTime < Date1 Then  ' <----- ERROR LINE
        If olItem.ReceivedTime > Date2 Then
        If InStr(olItem.Body, "Darth Vader") > 0 Then

        iAttachments = olItem.Attachments.Count + iAttachments

        Set olAttach = olItem.Attachments.Item(1)

        On Error GoTo Err_Handler
        olAttach.SaveAsFile "C:\Desktop\Automatic Outlook Downloads" & "\" & olAttach.Filename

        Set olAttach = Nothing
        Set olItem = Nothing


        If iAttachments = 4 Then Exit For
        End If
        End If
    End If
Next
David Zemens
  • 53,033
  • 11
  • 81
  • 130
1

So I was able to solve my own question. I'm not sure why my code was working 100% before today but I did make an adjustment so that I can have a more compatible syntax between Excel dates and Outlook dates. Below is my modified code that changes my Excel date format to match Outlooks date formats. Also, instead of "IF" conditions, I decided to restrict my olItems within my time frame and then loop for my conditions.

Dim olApp As Object
Dim olNS As Object
Dim myDate As Date
Dim olItems As Object
Dim olItem As Object
Dim olAttach As Object
Dim Date1 As String
Dim Date2 As String
Dim iAttachments As Integer

Date1 = Date & " " & TimeValue("6:00:00 am")
Date11 = Format(Date1, "ddddd h:nn AMPM")     <----- Date to match Outlook format
Date2 = Date & " " & TimeValue("00:00:00 am")
Date22 = Format(Date2, "ddddd h:nn AMPM")     <----- Date to match Outlook format


On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
Err.Clear: On Error GoTo 0

If olApp Is Nothing Then
Set olApp = CreateObject("Outlook.Application")
End If

Set olNS = olApp.GetNamespace("MAPI")



Set olItems = olNS.GetDefaultFolder(olFolderInbox).Items.Restrict("[ReceivedTime] > """ & Date22 & """ and [ReceivedTime] < """ & Date11 & """")     <----- Restricted my olItems to my specific range



For Each olItem In olItems
    If InStr(olItem.Body, "Darth Vader") > 0 Then

    iAttachments = olItem.Attachments.Count + iAttachments

    Set olAttach = olItem.Attachments.Item(1)

    On Error GoTo Err_Handler
    olAttach.SaveAsFile "C:\Desktop\Automatic Outlook Downloads" & "\" & olAttach.Filename

    Set olAttach = Nothing
    Set olItem = Nothing


    If iAttachments = 4 Then Exit For

    End If
Next


    Set olAttach = Nothing
    Set olItem = Nothing
    Set olApp = Nothing
    Set olNS = Nothing
    Set olItems = Nothing



Exit Sub
Computer_Nerd
  • 102
  • 11