VBA macro in NormalEmail.dotm

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?

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

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


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

    链接地址: http://www.djcxy.com/p/63756.html

    上一篇: 使用Microsoft Outlook 14.0对象库处理所有Outlook版本

    下一篇: NormalEmail.dotm中的VBA宏