Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Adding an autorun macro to a Word 2003 document template

151 views
Skip to first unread message

Chris Nelson

unread,
Oct 8, 2009, 12:54:09 PM10/8/09
to
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.

Rich007

unread,
Oct 8, 2009, 2:01:01 PM10/8/09
to
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

Chris Nelson

unread,
Oct 12, 2009, 9:24:22 AM10/12/09
to
On Oct 8, 2:01 pm, Rich007 <Rich...@discussions.microsoft.com> wrote:
> 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.

Chris Nelson

unread,
Oct 12, 2009, 9:44:18 AM10/12/09
to
On Oct 8, 2:01 pm, Rich007 <Rich...@discussions.microsoft.com> wrote:
> 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.

Chris Nelson

unread,
Oct 12, 2009, 10:05:58 AM10/12/09
to
On Oct 8, 2:01 pm, Rich007 <Rich...@discussions.microsoft.com> wrote:
> 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

Doug Robbins - Word MVP

unread,
Oct 12, 2009, 3:38:05 PM10/12/09
to
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 Nelson" <chris.ne...@gmail.com> wrote in message
news:5887a207-c241-4ab6...@j19g2000vbp.googlegroups.com...

Chris Nelson

unread,
Oct 13, 2009, 9:09:41 AM10/13/09
to
On Oct 12, 3:38 pm, "Doug Robbins - Word MVP"

<d...@REMOVECAPSmvps.org> wrote:
> 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.

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.

Chris Nelson

unread,
Oct 13, 2009, 9:46:47 AM10/13/09
to
On Oct 13, 9:09 am, Chris Nelson <chris.nelson.1...@gmail.com> wrote:
> On Oct 12, 3:38 pm, "Doug Robbins - Word MVP"
>
> <d...@REMOVECAPSmvps.org> wrote:
> > 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.
>
> 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.

Jay Freedman

unread,
Oct 13, 2009, 10:04:39 AM10/13/09
to

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 FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.


Chris Nelson

unread,
Oct 13, 2009, 10:22:37 AM10/13/09
to

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?

Chris Nelson

unread,
Oct 13, 2009, 10:23:14 AM10/13/09
to

Thanks. If I could get my macro to work at all now, I could go about
adding to it.

Doug Robbins - Word MVP

unread,
Oct 13, 2009, 3:34:35 PM10/13/09
to
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
"Chris Nelson" <chris.ne...@gmail.com> wrote in message

news:af96bc2a-a6b3-425b...@z34g2000vbl.googlegroups.com...

Chris Nelson

unread,
Oct 13, 2009, 4:04:29 PM10/13/09
to
On Oct 13, 3:34 pm, "Doug Robbins - Word MVP"

<d...@REMOVECAPSmvps.org> wrote:
> 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.

Chris Nelson

unread,
Nov 13, 2009, 10:00:37 AM11/13/09
to

I removed the AutoNew() macro and its module and added them back and
it works again. No clue why. VBA really needs a symbolic debugger.

Chris Nelson

unread,
Nov 13, 2009, 10:21:41 AM11/13/09
to
On Oct 13, 9:04 am, "Jay Freedman" <jay.freed...@verizon.net> wrote:
> Chris Nelson wrote:
> > On Oct 12, 3:38 pm, "Doug Robbins - Word MVP"
> > <d...@REMOVECAPSmvps.org> wrote:
> >> 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.
>
> > 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. Seehttp://www.word.mvps.org/FAQs/Userforms/CreateAUserForm.htmfor an
> introduction.

Thanks. That's starting to work. Is there a way I can validate the
input? At least to make sure it's non-blank?

Doug Robbins - Word MVP

unread,
Nov 13, 2009, 5:11:51 PM11/13/09
to
Use:

Sub AutoNew()
1 Dim Project As String
2 Project = InputBox("Enter the Project Number")
3 If Project <> "" Then
4 ActiveDocument.SaveAs "Project " & Project & " Notes.doc"
5 Else
6 MsgBox "Please Enter a valid Project Number."
7 GoTo 2
8 End If
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 Nelson" <chris.ne...@gmail.com> wrote in message

news:18b8447f-36e4-4989...@n35g2000yqm.googlegroups.com...

0 new messages