PDA

View Full Version : How Do I Get Autoexec to Run in an Embedded Word Document



Bobinnc
01-09-2007, 08:39 PM
I wrote an AutoExec macro that runs everytime Word is opened. However, when Word is invoked from another application, the AutoExec macro does not run. I suspect that this is a shell of Word. How can I make the AutoExec macro run automatically on opening an embedded Word Document when Word is not already opened?

Here is a simple example of my problem:

Initial Set-up:
1. Open Word
2. Add the following AutoExec macro to the Normal module:



Sub AutoExec()
MsgBox "Hello"
End Sub


3. Save Normal
4. Close Word

Test to Confirm AutoExec Macro:
1. Open Word
2. Confirm that the Message Box is displayed and okay it.
3. Close Word

Test to Confirm AutoExec Macro:
1. Open WordPad (other applications react in a similar fashion)
2. Type a few words and hit <enter>
3. Choose Menu Item "Insert", "Object..."
4. Pick "Create New", "Microsoft Word Document", check "Display as Icon" and press OK.
5. Microsoft Word Opens to a blank document, titled "Document in Document"
6. Note that the Message Box did not display.
7. Close Word
8. Close WordPad

Please lend your brains to help me solve this puzzle. Thanks.

BTW: I am using Word 2002 and running Windows XP

lucas
01-10-2007, 08:22 AM
I'm not sure if this will solve your problem but you might try putting the code in the ThisDocument Module and using:

Private Sub Document_Open()

TonyJollans
01-10-2007, 10:37 AM
Hi Bob,

Welcome to VBAX!

I'm afraid you can't do what you want. Auto macros are deliberately prevented from running when you invoke Word like that.

If it is being done from the UI there is, as far as I know, no way round it. If you are doing it from code you can explicitly run code in the document using (Application).Run.

Bobinnc
01-10-2007, 07:33 PM
Thanks for the feedback. Tony, I was afraid that it was an impossibility.

Lucas, for the our situation at work, I can't place any code in the embedded document, because the it will be a different document everytime.

I know I could attach the code in another macro (not an auto run one) in Normal.dot and require all the users to run the macro everytime they open the embedded Word document, But I don't believe that would do it. Also, the Boss doesn't want the users to have to take any action.

We are having problems with users placing too much information into the embedded document and it is really slowing the network down. We instructed the users to not paste high resolution photos as we found that about 5 Meg embedded file size is the limit, but they forget and it bogs the network. I thought I could solve the problem with my AutoExec macro that triggers a class module to trap the Document Close event. The event checks file size and if too large, prevent the close until the user reduces it. It worked great when you launch Word directly or already have Word open. But it does not work when Word launches when envoked from another application.

So the AutoExec macro idea is out. Any other ideas for solving this problem? Thanks - Bob