PowerUser
PowerUser

Reputation: 11791

Alternative to `CreateObject("Outlook.Application")`?

My intended process:

  1. This is on a VM running Windows 2008 Server r2
  2. Windows Task Scheduler kicks off an Access db with a startup macro.
  3. The VBA script generates and saves some highly customized emails.

The problem:

This scripts works just fine when I manually open the db and run the script. But when I use the task to start it, I get error 429 'ActiveX component can't create object' on the 2nd line of:

Dim OlApp As Outlook.Application
Set OlApp = CreateObject("Outlook.Application")

Why does this throw an error when initiated by the task scheduler, but run fine manually?

About the Task:

  1. On the General tab, I have these settings:
    a. Under my account
    b. with the highest privileges
    c. only when I'm logged on
  2. The action itself opens Access 2010 aka Office14 with the DB's path & name, /x, and macro name as parameters. When I run this as a batch file, there are no problems.
  3. I know almost nothing about this OS or the modern task scheduler, but I can always learn.

Update

Found an MS Support article on this very subject. It specifically states that CreateObject and CoCreateInstance will fail will the above error message if used in this way. However, no alternative is given.

Any suggestions, please?

Upvotes: 0

Views: 14060

Answers (4)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66276

No Office app, Outlook included, can be used from a service. Even if the task can interact with the desktop, the COM system will not let you connect to a running COM object (Outlook is a singleton) since the security contexts are different.

You can use Extended MAPI (C++ or Delphi), CDO 1.21 (deprecated and is no longer updated or installed) or Redemption (I am its author - use the RDO family of objects) - all of these load the Extended MAPI system in-proc instead of connecting to an out-of-proc COM object (exposed by outlook.exe).

Upvotes: 2

Byrin
Byrin

Reputation: 1

I have found a solution to your problem, The VBScript interpreter (cscript.exe/wscript.exe) comes with a 64-bit version of Windows and a 32-bit version.

What I did was installed the Office x86 then uninstalled then installed x64 tested emails from task scheduler working, then I reinstalled x86. I did restart after each install and uninstall. When you install the x64 and x86 version of office it will install both versions cscript.exe/wscript.exe x86 and x64. Which in the end allows you to send a mail from task scheduler (Tested on Windows 10).

Thanks

Upvotes: 0

Saran
Saran

Reputation: 3884

What fixed it for me was to run Outlook as Administrator.

Upvotes: 3

Gord Thompson
Gord Thompson

Reputation: 123779

When sending email messages from VBA code I have always preferred to use CDO instead of trying to automate Outlook. For some sample code to send email messages via CDO, look here.

Upvotes: 1

Related Questions