2
votes

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.
2
To follow up on justnS's comment, what line causes that error? Also note, if you reference the Outlook object library you'll need to qualify your references to "Application" like this: Word.Application, Outlook.Application, etc.JimmyPena
As I said, pretty much anything that references the Word object model - Application, Selection, ActiveDocument, etc. (though I don't get an error with ThisDocument etc. in the line above - it does however crash Outlook as I mentioned). So, if the entire method is this one line: "MsgBox TypeName(Application)", that'll do it. I still don't see any reason to reference the Outlook object library, when I want to modify the contents of a Word "document"...Gary McGill

2 Answers

0
votes

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

0
votes

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