0

I need to run VBA code on startup, and then on two minute intervals thereafter.

Private Sub Application_Startup()
    Call doThis
End Sub

Private Sub checkEmail(Item As Outlook.MailItem)
    'lots of code here
End Sub

Sub doThis()

    Dim myInbox                 As Outlook.Folder
    Dim ToPrint                 As Outlook.Folder
    Dim myNameSpace             As NameSpace
    Dim objItems                As Outlook.Items
    Dim objItem                 As Outlook.MailItem
    
    Set myNameSpace = Application.GetNamespace("MAPI")
    Set ToPrint = myNameSpace.GetDefaultFolder(olFolderInbox).Folders("ToPrint")
    Set objItems = ToPrint.Items

    For Each objItem In objItems
        Call checkEmail(objItem)
    Next

    Application.Ontime Now + TimeValue("00:02:00"), "doThis"

    Set myInbox = Nothing
    Set ToPrint = Nothing
    Set myNameSpace = Nothing
    Set objItem = Nothing
    Set objItems = Nothing

End Sub

When I get to this line:

Application.Ontime Now + TimeValue("00:02:00"), "doThis"

I get

Run-time error 438:
"Object doesn't support this property or method."

Community
  • 1
  • 1
  • [Run a code every half an hour](https://stackoverflow.com/questions/12257985/outlook-vba-run-a-code-every-half-an-hour) – niton Jul 27 '22 at 16:15

1 Answers1

1

Outlook does not support the Application.Run or Application.OnTime methods.

But you could use the OnTime method from an Object model that does support it, like Excel. That is, you'd automate Outlook from Excel, but depending on what you want to achieve in Outlook, you might be blocked by the Outlook Object Model Guard.

ThunderFrame
  • 9,352
  • 2
  • 29
  • 60
  • Thank you! I will probably switch to a script that will run on all incoming emails, which was always a (slightly worse) option. – richardsonralph Apr 03 '16 at 20:46