Outlook 2007 - NormalEmail.dotm 中的 VBA 宏

发布于 2024-12-15 07:30:28 字数 1215 浏览 1 评论 0原文

我有一个 Word 2007 模板,用作 Normal.dotm,其中包含企业主题、样式等,以及一些覆盖项目符号和注释的默认行为的宏代码。功能区上的数字按钮。

我现在想在 Outlook 2007 中创建电子邮件时使用相同的模板。我尝试将 Normal.dotm 复制到 NormalEmail.dotm,并且在一定程度上这是有效的,但是尽管我的宏被正确调用 /em> 当我按电子邮件编辑器中的项目符号/数字按钮时,会出现错误:

运行时错误 429:ActiveX 组件无法创建对象

经过进一步调查,我在宏中所做的任何引用普通 Word 对象模型(应用程序、选择等)中的对象的操作似乎都会导致此错误。因此,即使我的宏包含以下内容,它仍然会失败:

MsgBox TypeName(Application)

我有一种暗示,这是因为我在 Outlook 上下文中运行,而不是在 Word 中运行,因此可能没有(Word)应用程序,或者任何与它相关的对象。我知道 Outlook 中的电子邮件编辑器是 Word,但不是我们所知道的 Jim。

话虽如此,我确实发现 ThisDocument 确实返回一个有效的 Document 对象;不幸的是,它对应于模板本身,而不是正在编辑的电子邮件。

通过反复试验,我发现我可以通过以下迂回路线获取与电子邮件相对应的文档(这会显示电子邮件中的文本):

MsgBox ThisDocument.MailEnvelope.Item.Application.ActiveInspector.WordEditor.Content.Text

但是,此代码似乎也会导致 Outlook 崩溃 - 不是当时崩溃,而是稍后当您关闭 Outlook 时崩溃。

此时此刻,我已经做好了认输的准备。毫无疑问,在 Outlook 中创建宏的“正确”方法是在 Outlook 本身中创建它们,但从我有限的经验来看,这太糟糕了。 (您可以在 VBA 中创建宏,但无法部署它们;或者,您可以在 VSTO 中创建它们,但您需要一个安装程序,而不是简单地部署模板)。

所以。有什么想法可以让我做我想做的一切吗?

  1. 覆盖电子邮件编辑器中功能区上项目符号/数字按钮的行为。
  2. 在 NormalEmail.dotm 文件中部署宏,而不必“安装”它们。
  3. 不会使 Outlook 崩溃。

I have a Word 2007 template that I use as Normal.dotm, which has corporate theme, styles, etc., plus a tiny bit of macro code that overrides the default behaviour of the bullet & number buttons on the ribbon.

I'd now like to use this same template when creating emails in Outlook 2007. I've tried just copying my Normal.dotm to NormalEmail.dotm, and to an extent this works, but although my macro is correctly invoked when I press the bullet/number buttons in the email editor, it gives an error:

Run time error 429: ActiveX component can't create object

On further investigation, it seems that anything I do within my macro that references an object in the normal Word object model (Application, Selection, etc.) causes this error. So, even if my macro consists of the following, it still fails:

MsgBox TypeName(Application)

I have an inkling that this is because I'm running in the context of Outlook, not Word, and so perhaps there is no (Word) application, or any of the objects associated with it. I know that the email editor in Outlook is Word-but-not-as-we-know-it-Jim.

Having said that, I did discover that ThisDocument does return a valid Document object; unfortunately, it corresponds to the template itself, not the email being edited.

By trial and error, I've discovered that I can get at the Document corresponding to the email by the following circuitous route (this displays the text in the email):

MsgBox ThisDocument.MailEnvelope.Item.Application.ActiveInspector.WordEditor.Content.Text

However, this code also seems to cause Outlook to crash - not at the time, but later, when you close Outlook.

At this point, I'm just about ready to admit defeat. No doubt the "correct" way to create macros in Outlook is to create them in Outlook itself, but from my limited experience, that's just horrible. (You can create macros in VBA, but you can't deploy them; or, you can create them in VSTO, but then you need an installer rather than simply deploying a template).

So. Any ideas out there that would let me do all that I hoped to do?

  1. Override the behaviour of the bullet/number buttons on the ribbon in the email editor.
  2. Deploy the macros in the NormalEmail.dotm file, rather than having to "install" them.
  3. Not crash Outlook.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诗笺 2024-12-22 07:30:29

您是否尝试过将 VBA 编辑器中的引用添加到 Microsoft Outlook 对象库?

Have you tried adding the reference in the VBA editor to the Microsoft Outlook Object Library?

徒留西风 2024-12-22 07:30:29

在 Outlook 2007 中,您可能还需要添加对“Microsoft Word 12.0 对象库”或者“Microsoft Forms 2.0 对象库”的 VBA 引用。不同的版本可能适用于其他版本年份。

此外,在宏/代码中,您必须设置对象才能获取/使用正确的方法和属性。

我不是一个面向对象的程序员,通常从其他地方复制代码并根据我的需要进行修改,所以我无法提供比上面所说的更多的帮助。 (我的一个宏也给了我同样的 429 错误。)

In Outlook 2007 you might also have to add VBA references to "Microsoft Word 12.0 Object Library" and maybe "Microsoft Forms 2.0 Object Library". Different versions may apply for other version years.

In addition, in the macro/code you have to set the objects to get/use the correct methods and properties.

I'm not an OO programmer and usually copy code from elsewhere and modify for my need so I can't help much more than what I said above. (And one of my macros is also giving me the same 429 error you get.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文