Autoit ObjGet Outlook.Application fails with Unknown Name (0x800020006) when run from Task Scheduler

0

I have Outlook 2016 running. The following script runs without a problem from SciTE or the command line. However, the script fails when run from Task Scheduler. Why does it fail when run from Task Scheduler? What can I do to fix this?

Script:

#include <MsgBoxConstants.au3>

$error   = ObjEvent("AutoIt.Error", "ErrFunc")
$outlook = ObjGet("", "Outlook.Application")

If @error Then
   MsgBox($MB_SYSTEMMODAL, "", "Delete Pipeline Emails" & @CRLF & "Error getting an active Outlook object. Error code: " & Hex(@error, 8))
   exit 1
EndIf

Note: ErrFunc removed for brevity.

Error:

    err.number is:          0x80020006
    err.windescription:     Unknown name.
    err.description is:
    err.source is:
    err.helpfile is:
    err.helpcontext is:
    err.lastdllerror is:    0
    err.scriptline is:      -1
    err.retcode is:         0x00000000
outlook
com
autoit
asked on Stack Overflow Oct 14, 2018 by Nathan • edited Oct 14, 2018 by Nathan

1 Answer

0

The trick is to close the Outlook process and then try ObjCreate() again. This will create a process but no window. To show a window, the simplest solution is to run outlook.exe.

$outlook = ObjGet("", "Outlook.Application")

if @error then
   $outlook = ObjCreate("Outlook.Application")

   if @error then
      ProcessClose("outlook.exe")

      $outlook = ObjCreate("Outlook.Application")
   endif
endif
answered on Stack Overflow Oct 15, 2018 by Nathan

User contributions licensed under CC BY-SA 3.0