简体   繁体   English

使用Excel VBA通​​过模板在Outlook 2010中创建电子邮件

[英]Using Excel VBA to create email in Outlook 2010 from template

I have code that worked well in Excel/Outlook 2003 on XP, I'm now running Windows 7 Excel/Outlook 2010 and receive an error: 我的代码在XP上的Excel / Outlook 2003中运行良好,现在正在运行Windows 7 Excel / Outlook 2010,并收到错误消息:

Run-time error '287': Application-defined or object-defined error. 运行时错误“ 287”:应用程序定义或对象定义的错误。

My code is based on this answer in: Send an email from Excel 2007 VBA using an Outlook Template & Set Variables 我的代码基于以下答案: 通过使用Outlook模板和设置变量从Excel 2007 VBA发送电子邮件

I'm creating my outlook object and mailitem like this: 我正在创建我的Outlook对象和mailitem,如下所示:

Dim MyOlApp
Dim MyOlItem
Set MyOlApp = CreateObject("Outlook.Application")
Set MyOlItem = MyOlApp.CreateItemFromTemplate(Range("oftLocation").Value) 'user defined location

The error appears after I reference MyOlItem , the code is replacing references in the .htmlBody 我引用MyOlItem后出现错误,该代码替换了.htmlBody中的.htmlBody

Amending to .Body makes no difference, same error shows. 修改为.Body并没有区别,显示相同的错误。

My references in VBA are set to: 我在VBA中的引用设置为:

  • VBA VBA
  • Microsoft Excel 14.0 Object Library Microsoft Excel 14.0对象库
  • OLE Automation OLE自动化
  • Microsoft Office 14.0 Object Library Microsoft Office 14.0对象库
  • Microsoft Forms 2.0 Object Library Microsoft Forms 2.0对象库
  • Microsoft Scripting Runtime Microsoft脚本运行时

This isnt an answer yet but I want to collect what is happening in the comments since I feel this is the best direction with what I have seen thus far. 这还不是答案,但是我想收集评论中正在发生的事情,因为我认为这是到目前为止所看到的最好的方向。

I have tested the code in you have in your question and it works just fine for me using Outlook 2010 and Excel 2010. 我已经测试过您问题中的代码,并且使用Outlook 2010和Excel 2010对我来说效果很好。

When Excel tries to interact with Outlook the Programmatic Access settings come into play. 当Excel尝试与Outlook进行交互时,Programmatic Access设置将发挥作用。

警告信息

I beleive they are setting to a warning state if you AntiVirus is out of date. 我相信,如果您的杀毒软件已过期,它们将设置为警告状态。 This is similar to the way that Windows detects the status of your AntiVirus. 这类似于Windows检测AntiVirus状态的方式。 So when you access Outlook from Excel normally get a prompt asking permissiong for the action. 因此,当您从Excel访问Outlook时,通常会出现一个提示,要求获得许可g。 In my case, and your under normal circumstances, you would allow the action and the code would proceed as normal. 就我而言,在正常情况下,您将允许该操作,并且代码将正常进行。

However I have seen people catch error 287 with the logic that the user hit No to that prompt. 但是,我看到人们发现错误287的逻辑是用户在该提示下单击否。 This does not appear to be the case for you. 似乎并不适合你的情况。 You need to take a good look at your Trust Center settings in Outlook where Programmatic Access in concerned. 您需要仔细查看Outlook中与Programmatic Access有关的Trust Center设置。 There is interaction with antivirus and the Trust Center will provide some information as well. 与防病毒软件有交互作用,信任中心也将提供一些信息。 Settings there will be disabled if you are not running Outlook with administrator rights. 如果您未使用管理员权限运行Outlook,则将禁用该设置。 Let me know what you come up with. 让我知道你的想法。

I havent been explicit up until now but I would like to draw attention to your AV as well. 到目前为止,我还没有明确表示出来,但是我也想提请您注意AV。 This is one of the things that it would try to stop. 这是它试图停止的事情之一。 Perhaps there is a rule that fired that is preventing this. 也许有一个被炒鱿鱼的规则阻止了这种情况。

Either way you should be able to test this in another environment to see if it maybe your system in particular. 无论哪种方式,您都应该能够在另一个环境中进行测试,以查看它是否特别是您的系统。 I think we both agree it is not the code specifically since I know it works elsewhere. 我想我们都同意这不是专门的代码,因为我知道它可以在其他地方使用。

Can I suggest that you may need a copy of Outlook open and running. 我可以建议您可能需要打开并运行Outlook副本。 The use of CreateObject() may be insufficient. 使用CreateObject()可能不够。

To do this programmatically, amend you VBA macro as follows: 若要以编程方式执行此操作,请如下修改VBA宏:

Shell "Outlook.exe" 'Opens Outlook

' Your existing code goes here

Shell "taskkill /IM Outlook.exe" 'Closes Outlook again when code is finished (optional)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM