run a macro automatically when you create a new document

D

Dovbniak

I have tried creating a template with a dialog box that I want shown when a
user creates a new document using that template. I create a macro called
AutoNew() but when I create the new document using the template, it does not
run. I am using Word 2003. When I go to Tools, Maco, Run and run the
AutoNew() macro, it runs fine. I have also tried AutoOpen just for the heck
of it (to run when opening a saved file) and they do not run either.

Any ideas?
 
G

Gordon Bentley-Mix

Post your code and perhaps someone can spot the problem.

--
Gordon Bentley-Mix
Word MVP

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
A

alborg

Hi Dovbniak:

What you have to do is to-

1) Make sure that your document is the template (not the doc file).
2) Go to the VBA Editor platform, YourProjectName-> Microsoft Word Objects.
3) Left click on it, then select Insert-> Module. It automatically is named
"Module1", but you can rename it to anything you wish, but for this exercise
leave it as "Module1".
4) Add your macro here (in the Module1 code pane).

Sub AutoNew()
'-------- the autonew code --------
End Sub

5) Now go to and doubleclick on YourProjectName-> Microsoft Word Objects->
ThisDocument
6) Place the following sub routine that begins and ends with the following-

Private Sub Document_New()
Call Module1.AutoNew
End Sub

Alternatively, forget the AutoNew macro and cut out the code and place it as
such-

Private Sub Document_New()
....Your AutoNew macro code goes here...
End Sub

Now when you get to a new document based on the template, your code will
execute each and every time!

Cheers,
Al
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top