Adding an autorun macro to a Word 2003 document template

C

Chris Nelson

Maybe I'm going about this the wrong way. I'm an experienced software
engineer but I've done almost no application automation in Office.
Any pointers to general techniques and approaches would be welcome.
I've searched local and on-line Office help and this group without
getting any strong pointers.

I want to create a template document that when opened (or perhaps when
saved) prompts the user for some information and forms the document's
name out of that information filled into a template. Say I have
"Project Notes.dot" I want to guide the user to saving their notes as
"Project xxxx Notes.doc" where xxxx is a project number that the user
provides. (Bonus points if I can also put some of the user-supplied
information into the document properties like Keywords and Comments or
custom fields.)

TIA.
 
R

Rich007

Chris,
You can use application events to run code when a document is created,
opened or saved. Have a look on the Word MVP page here:
http://word.mvps.org/FAQs/MacrosVBA/AppClassEvents.htm

You could also look at
http://word.mvps.org/FAQs/MacrosVBA/ApplicationEvents.htm
and
http://word.mvps.org/FAQs/MacrosVBA/DocumentEvents.htm
but the Application Events has worked well for me.

There is also a section on that site on "Working with properties", see
http://word.mvps.org/FAQs/MacrosVBA/index.htm.
(it's the one under Working with Events...).

You can get info from the user via the InputBox function (see VBA's help on
that).

I think everything you need is there. Good luck.

Cheers
Rich
 
C

Chris Nelson

Chris,
You can use application events to run code when a document is created,
opened or saved.  Have a look on the Word MVP page here:http://word.mvps.org/FAQs/MacrosVBA/AppClassEvents.htm

I appreciate the pointer but I get stuck on step 2 which says, "Rename
the Class Module from Class1 to ThisApplication" without any clue how
to do that. I've tried every menu option, a context menu in the
navigation pane, a context menu in the class window, highlighting
Class1 and pressing F2. Nothing gets met close to being able to
rename the class. I searched help for "how do I rename a class" and
got no useful results.
 
C

Chris Nelson

Chris,
You can use application events to run code when a document is created,
opened or saved.  Have a look on the Word MVP page here:http://word.mvps.org/FAQs/MacrosVBA/AppClassEvents.htm

You could also look athttp://word.mvps.org/FAQs/MacrosVBA/ApplicationEvents.htm
andhttp://word.mvps.org/FAQs/MacrosVBA/DocumentEvents.htm
but the Application Events has worked well for me.

There is also a section on that site on "Working with properties", seehttp://word.mvps.org/FAQs/MacrosVBA/index.htm.
(it's the one under Working with Events...).

You can get info from the user via the InputBox function (see VBA's help on
that).

I think everything you need is there.  Good luck.

I guess I'm missing something fundamental. I followed those
directions except that I don't want my macro to be universal, I want
it only in a specific template so I put it there, not in the Word
startup directory. I have

Private Sub oApp_NewDocument(ByVal Doc As Document)
Dim Result
Result = InputBox("Title", "foo")
End Sub

which did nothing, so I moved my InputBox to the AutoExec():

Public Sub AutoExec()
Set oAppClass.oApp = Word.Application
Dim Result
Result = InputBox("Title", "foo")

End Sub

What I hope to see is when I double-click on the .dot containing this
macro that a InputBox would be displayed. I don't see anything.
 
C

Chris Nelson

Chris,
You can use application events to run code when a document is created,
opened or saved.  Have a look on the Word MVP page here:http://word.mvps.org/FAQs/MacrosVBA/AppClassEvents.htm

You could also look athttp://word.mvps.org/FAQs/MacrosVBA/ApplicationEvents.htm
andhttp://word.mvps.org/FAQs/MacrosVBA/DocumentEvents.htm
but the Application Events has worked well for me.

There is also a section on that site on "Working with properties", seehttp://word.mvps.org/FAQs/MacrosVBA/index.htm.
(it's the one under Working with Events...).

You can get info from the user via the InputBox function (see VBA's help on
that).

I think everything you need is there.  Good luck.

I guess I'm missing something fundamental. I followed those
directions except that I don't want my macro to be universal, I want
it only in a specific template so I put it there, not in the Word
startup directory. I have

Private Sub oApp_NewDocument(ByVal Doc As Document)
Dim Result
Result = InputBox("Title", "foo")
End Sub

which did nothing, so I moved my InputBox to the AutoExec():

Public Sub AutoExec()
Set oAppClass.oApp = Word.Application
Dim Result
Result = InputBox("Title", "foo")

End Sub

What I hope to see is when I double-click on the .dot containing this
macro that a InputBox would be displayed. I don't see anything.
 
D

Doug Robbins - Word MVP

All that you need do is insert a module into your template and in that
module, create the following macro

Sub AutoNew()
Dim Project as string
Project = InputBox("Enter the Project Number")
ActiveDocument.SaveAs "Project " & Project & " Notes.doc"
End Sub

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
Chris,
You can use application events to run code when a document is created,
opened or saved. Have a look on the Word MVP page
here:http://word.mvps.org/FAQs/MacrosVBA/AppClassEvents.htm

You could also look
athttp://word.mvps.org/FAQs/MacrosVBA/ApplicationEvents.htm
andhttp://word.mvps.org/FAQs/MacrosVBA/DocumentEvents.htm
but the Application Events has worked well for me.

There is also a section on that site on "Working with properties",
seehttp://word.mvps.org/FAQs/MacrosVBA/index.htm.
(it's the one under Working with Events...).

You can get info from the user via the InputBox function (see VBA's help
on
that).

I think everything you need is there. Good luck.

I guess I'm missing something fundamental. I followed those
directions except that I don't want my macro to be universal, I want
it only in a specific template so I put it there, not in the Word
startup directory. I have

Private Sub oApp_NewDocument(ByVal Doc As Document)
Dim Result
Result = InputBox("Title", "foo")
End Sub

which did nothing, so I moved my InputBox to the AutoExec():

Public Sub AutoExec()
Set oAppClass.oApp = Word.Application
Dim Result
Result = InputBox("Title", "foo")

End Sub

What I hope to see is when I double-click on the .dot containing this
macro that a InputBox would be displayed. I don't see anything.
 
C

Chris Nelson

All that you need do is insert a module into your template and in that
module, create the following macro

Sub AutoNew()
    Dim Project as string
    Project = InputBox("Enter the Project Number")
    ActiveDocument.SaveAs "Project " & Project & " Notes.doc"
End Sub

Yes! That's a great start. Thanks.

I need to build on this in two ways. First, I need to change all
instances of a sentinal string to the project number. I can spelunk
VBA help to figure out how to automate search and replease. Second,
I'd really like to be able to enter a project description, too. Is
there something like InputBox that lets me create more complex dialog
boxes where the user can enter multiple values? Another approach I've
considered is creating a Project Wizard but I have no idea how Office
wizards (like for resumes, etc.) are created.
 
C

Chris Nelson

Yes!  That's a great start.  Thanks.
...

That worked once or twice and stopped working. :-( I removed all my
recorded macros and other possible noise and it still doesn't work.
 
J

Jay Freedman

Chris said:
Yes! That's a great start. Thanks.

I need to build on this in two ways. First, I need to change all
instances of a sentinal string to the project number. I can spelunk
VBA help to figure out how to automate search and replease. Second,
I'd really like to be able to enter a project description, too. Is
there something like InputBox that lets me create more complex dialog
boxes where the user can enter multiple values? Another approach I've
considered is creating a Project Wizard but I have no idea how Office
wizards (like for resumes, etc.) are created.

What you're describing is a UserForm, which is a custom dialog box with
associated VBA code. See
http://www.word.mvps.org/FAQs/Userforms/CreateAUserForm.htm for an
introduction.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
C

Chris Nelson

That worked once or twice and stopped working. :-(  I removed all my
recorded macros and other possible noise and it still doesn't work.

The first place I got this to work was in a template in my normal
template directory. Then I copied it to another directory where I
plan to be able to create documents by double-clicking on the .dot in
Explorer. I haven't touched the original test but it fails now, too.
Do they interact with Normal.dot somehow? Is there some global
environment I broke by cleaning up Normal.dot along the way?
 
D

Doug Robbins - Word MVP

The location where you have placed the template is probably not a trusted
location and your macro security settings are probably set to automatically
disable macros without notification when a file is opened/created from such
a location.

You should put the template in your User or Workgroup Templates folder so
that it is trusted and I would suggest that you use File>New to create new
documents.

As Jay said, a UserForm will allow you to enter more information at the time
of creating a new document and is preferrable in my opinion to the use of
multiple InputBox commands.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
That worked once or twice and stopped working. :-( I removed all my
recorded macros and other possible noise and it still doesn't work.

The first place I got this to work was in a template in my normal
template directory. Then I copied it to another directory where I
plan to be able to create documents by double-clicking on the .dot in
Explorer. I haven't touched the original test but it fails now, too.
Do they interact with Normal.dot somehow? Is there some global
environment I broke by cleaning up Normal.dot along the way?
 
C

Chris Nelson

The location where you have placed the template is probably not a trusted
location and your macro security settings are probably set to automatically
disable macros without notification when a file is opened/created from such
a location.

I'd believe you if it never worked. But it worked for a while in my
non-template location and stopped working in my template folder, too.
You should put the template in your User or Workgroup Templates folder so
that it is trusted and I would suggest that you use File>New to create new
documents.

That's really not practical for my application.
As Jay said, a UserForm will allow you to enter more information at the time
of creating a new document and is preferrable in my opinion to the use of
multiple InputBox commands.

I agree. And if I can get a macro to execute again, I'll look into
UserForm.
 

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