How do I get a Macro to open automatically?

M

Matt

I've created a form that users fill in for a template
that automatically drops info into the header and title.

I've coded everything and I've run the code and it works
perfectly. What I can't figure out is how do I get the
code to work every time a user opens the template and a
new document is created? The form should open
automatically when the user clicks on the template.

Here is the code I tried:

Public Sub Document_New()
Relfrm.Show
End Sub

I'd really appreciate some help on this.

Thank You,

Matt
 
J

Jonathan West

Hi Matt

Call your macro AutoNew, and it will run whenever a document is created that
is based on the template.

Call your macro AutoOpen, and it will run whenever a document is opened that
is based on the template.

For more on this, take a look here.

Running a macro automatically when a document is created, opened or closed
http://word.mvps.org/FAQs/MacrosVBA/DocumentEvents.htm
 
C

Charles Kenyon

If the code you listed is in the template's "This Document" object it will
run when you create a new document based on the template. I just tested the
following:

Public Sub Document_New()
MsgBox "New Document Created"
End Sub

Public Sub Document_Open()
MsgBox "Existing Document Opened"
End Sub

The first runs when the new document is created. The second runs when that
document is opened from the disk.

You may also want to run a search in the userforms newsgroup on Google for
the term "magic forms." I am not qualified in any way to address what, if
any, problems direct use of a form can cause. I know the language I've seen
suggested is along the lines of:

Dim myForm As Relfrm
Set myForm = New Relfrm
myForm.Show
Unload myForm
Set myForm = Nothing

What I read at the time convinced me that this was worth the extra effort
and I've been doing it since then, but I can't tell you why now. I believe
it primarily has to do with use of the template by multiple people
simultaneously or with having different documents open with the same
template.
--

Charles Kenyon

See the MVP FAQ: <URL: http://www.mvps.org/word/> which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 

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