vba 替代`CreateObject("Outlook.Application")`?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18921165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 23:26:38  来源:igfitidea点击:

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

vbams-accessaccess-vbascheduled-tasksoutlook-vba

提问by PowerUser

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.
  1. 这是在运行 Windows 2008 Server r2 的虚拟机上
  2. Windows 任务计划程序使用启动宏启动 Access 数据库。
  3. VBA 脚本生成并保存一些高度定制的电子邮件。

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:

当我手动打开数据库并运行脚本时,此脚本工作正常。但是,当我使用该任务启动它时,在第二行出现错误 429“ActiveX 组件无法创建对象”:

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.
  1. 在常规选项卡,我有以下设置:
    一。在我的帐户下
    B. 拥有最高权限
    C. 仅当我登录时
  2. 该操作本身会打开 Access 2010 aka Office14,并将数据库的路径和名称、/x 和宏名称作为参数。当我将它作为批处理文件运行时,没有问题。
  3. 我对这个操作系统或现代任务调度程序几乎一无所知,但我总是可以学习。

Update

更新

Found an MS Supportarticle on this very subject. It specifically states that CreateObjectand CoCreateInstancewill fail will the above error message if used in this way. However, no alternative is given.

找到了一篇关于这个主题的MS 支持文章。它特别指出CreateObjectCoCreateInstance如果以这种方式使用将失败将上述错误消息。但是,没有给出替代方案。

Any suggestions, please?

请问有什么建议吗?

回答by Gord Thompson

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.

从 VBA 代码发送电子邮件时,我一直更喜欢使用 CDO 而不是尝试自动化 Outlook。有关通过 CDO 发送电子邮件的一些示例代码,请查看此处

回答by Dmitry Streblechenko

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.

不能从服务中使用任何 Office 应用程序(包括 Outlook)。即使任务可以与桌面交互,COM 系统也不会让您连接到正在运行的 COM 对象(Outlook 是一个单例),因为安全上下文是不同的。

You can use Extended MAPI (C++ or Delphi), CDO 1.21 (deprecated and is no longer updated or installed) or Redemption(use the RDOfamily 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).

您可以使用扩展 MAPI(C++ 或 Delphi)、CDO 1.21(已弃用且不再更新或安装)或Redemption(使用RDO系列对象)——所有这些都在进程内加载扩展 MAPI 系统,而不是连接到进程外 COM 对象(由 Outlook.exe 公开)。

回答by Saran

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

为我修复的是以管理员身份运行 Outlook

回答by Byrin

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.

我找到了解决您的问题的方法,VBScript 解释器 (cscript.exe/wscript.exe) 带有 64 位版本的 Windows 和一个 32 位版本。

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).

我所做的是安装 Office x86,然后卸载然后安装 x64 测试的来自任务计划程序工作的电子邮件,然后我重新安装了 x86。每次安装和卸载后我都重新启动。当您安装 x64 和 x86 版本的 office 时,它​​将同时安装 cscript.exe/wscript.exe x86 和 x64 版本。这最终允许您从任务计划程序发送邮件(在 Windows 10 上测试)。

Thanks

谢谢