PDA

View Full Version : Best practice to run a macro in a word template



Johnosley
08-03-2010, 09:11 AM
My userform is ready and is working. I saved everything as a word template.

Now I'm wondering what is the best practice to run the macro that called the userform ?

I'm asking the experts for that and would like to know that people do to avoid trouble with users who don't know anything about macro and make sure that the macro will work properly and will be easily available anytime.

Thanks
John.

gmaxey
08-03-2010, 12:12 PM
John,

When is your UserForm supposed to be displayed? If it is on creation of a new document based on your template then you would place it in the ThisDocument module of the template as a Sub Document_New() procedure. Something like this:

Private Sub Document_New()
Dim oFrm As UserForm 'Replace UserForm with the specific name of your form
Set oFrm = New UserForm 'Replace UserForm with the specific name of your form
oFrm.Show
Unload oFrm
Set oFrm = Nothing
End Sub

Johnosley
08-03-2010, 03:18 PM
John,

When is your UserForm supposed to be displayed? If it is on creation of a new document based on your template then you would place it in the ThisDocument module of the template as a Sub Document_New() procedure.

I've done that already. But users could launch the userform anytime after the creation of the new document to change their answers for example. I'm thinking about adding into my template a command button such as active X control. But don't know if this is the best way to do that.

what could be the benefit of using an add-in for example ?

fumei
08-03-2010, 03:55 PM
1. if the userform is in the template then the user MUST have access to the template to execute the instruction to show the userform. That is, unless you import the userform (and a procedure to show it) into the document.

2. add-ins are good.

3. you could put the icon on a toolbar (if you are not using the spawn of satan version 2007/2010) and let the user bring up the userform with a click; or a menu item (if you are not using the spawn of satan version 2007/2010)

fumei
08-03-2010, 04:00 PM
Greg....<grin>...

Private Sub Document_New()
Dim oFrm As UserForm 'Replace UserForm with the specific name of your form
Set oFrm = New UserForm 'Replace UserForm with the specific name of your form
oFrm.Show
Unload oFrm
Set oFrm = Nothing
End Sub
Tell me the advantage of that over
Private Sub Document_New()
oFrm.Show
End Sub
Assuming of course that there is an proper Unload Me instruction on the userform. I never do anything but give the .Show instruction in my _New events.

Johnosley
08-03-2010, 06:15 PM
3. you could put the icon on a toolbar (if you are not using the spawn of satan version 2007/2010) and let the user bring up the userform with a click; or a menu item (if you are not using the spawn of satan version 2007/2010)

unfortunately I'm using 2007 !

For the add-in, could you let me know the pros and cons ?

gmaxey
08-03-2010, 06:56 PM
I am an old dog and just .Show is a new trick. It is an old habit hard to break. I can't point to any advantage.

gmaxey
08-03-2010, 06:58 PM
John,

You can add the macro that displays the userform to the Quick Access Toolbar. http://gregmaxey.mvps.org/Add_Macro_To_QAT.htm

Johnosley
08-04-2010, 07:10 AM
John,

You can add the macro that displays the userform to the Quick Access Toolbar. http://gregmaxey.mvps.org/Add_Macro_To_QAT.htm

I think I will try both solution "add-in" and "QAT" and see which one works the best for me.

Thanks