Checking for the existence of a Macro

  • Thread starter NZ VBA Developer
  • Start date
N

NZ VBA Developer

Here's a curly one for you all!

I have a series of standard 'office practice' type templates (Fax, Letter,
Memo, etc.) all of which contain code for creating the document in the first
place. This code saves the input in document variables to allow the code to
be rerun should the user get to the end and realise they forgot something.
The rerun code works a treat - or at least it does in the one prototype
template that I've done so far. ;)

I also have a global 'toolkit' type template that's designed to make doing
some key functions easier or just make Word work like it should work: e.g.
insert/delete a landscape section without stuffing up everything; apply
bullets/numbering using the 'List' styles instead of whatever Word wants to
use; copy/reset the standard styles in a document. This works really well,
too.

But now for the tricky part...

I know I could provide access to the rerun macro in each template in a
variety of ways: add a custom button a standard toolbar or create a custom
toolbar for each template or some such. However, since I'd rather not mess
around with any of the standard toolbars (and have my Rerun Template button
get lost in the clutter) and since I don't want to create a bunch of 1-button
toolbars and since the toolkit already has a custom toolbar, what I'd really
rather do is have a button on the Toolkit toolbar for invoking the rerun
macro. But I have some concerns and questions.

First, as would be expected, if just add a button to the Toolkit toolbar for
the rerun macro in a specific template, it only works with a document based
on that particular template. And since there are 11 templates, I would have
to add 11 buttons and then trust the users (stop LAUGHING!!!) to click the
right one or try to trap errors or show/hide buttons based on the attached
template for a document... and I don't even want to think about what would
happen if there were multiple documents open... GAHHH!!!

So I reckon a better approach would be to have a macro (and associated
button) in the toolkit template that just calls the rerun macro in the
attached template. I can guarantee that the name of the macro in each
template would be the same, so that's no problem. However, I'm
concerned/unclear about: 1) how to check that the attached template does
indeed contain a rerun macro; and 2) the actual syntax for calling the macro
in the attached template.

Suggestions? Recommendations? Or have I just spent too much time with the
sheep and am now delusional to even consider it?
 
S

Shauna Kelly

Hi

Clearly far, far too many sheep <g>.

There are lots of solutions to this. Here's one solution that I've used in
similar circumstances.

Create one .dot, store it in the Word Startup folder and put *all* your
re-run code in it. Have one re-run button. Start the code called by the
re-run button code with something like:

Select Case ActiveDocument.AttachedTemplate
Case "Letter.dot"
'do letterish things
Case "Fax.dot"
'do faxy things
Case "someother.dot"
'whatever
Case Else
msgbox "Sorry, that doesn't work in this kind of document."
End Select

What I like about this method is:
(a) I don't have to worry about broken references
(b) I have all the code in one file which makes it easier to write, maintain
and distribute
(c) I can use common sub-routines to reduce the total amount of code, which
makes it easier to write, maintain and distribute
(d) Users don't need to make a choice or remember anything.

If required, you can still put a template-specific toolbar that invokes
template-specific tools in any individual template.

Alternatives are:
- In your add-in, in the VBE, use Tools > References to create a reference
to the template. Then call the individual code in the template(s).
- In your add-in use Application.Run to call individual Subs in the
template(s).

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
N

NZ VBA Developer

Sweet as!

I've been experimenting a bit and reckon the best approach lies somewhere in
between your suggestions - a Case statement and a Run command to call the
code in the individual templates. This will probably work just fine since the
rerun code doesn't do much more than load, initialise and show the userform,
so nothing much to write, maintain or distribute and not much common code
either.

Time to leave the sheep alone and get busy!
--
Cheers!

The Kiwi Koder


Shauna Kelly said:
Hi

Clearly far, far too many sheep <g>.

There are lots of solutions to this. Here's one solution that I've used in
similar circumstances.

Create one .dot, store it in the Word Startup folder and put *all* your
re-run code in it. Have one re-run button. Start the code called by the
re-run button code with something like:

Select Case ActiveDocument.AttachedTemplate
Case "Letter.dot"
'do letterish things
Case "Fax.dot"
'do faxy things
Case "someother.dot"
'whatever
Case Else
msgbox "Sorry, that doesn't work in this kind of document."
End Select

What I like about this method is:
(a) I don't have to worry about broken references
(b) I have all the code in one file which makes it easier to write, maintain
and distribute
(c) I can use common sub-routines to reduce the total amount of code, which
makes it easier to write, maintain and distribute
(d) Users don't need to make a choice or remember anything.

If required, you can still put a template-specific toolbar that invokes
template-specific tools in any individual template.

Alternatives are:
- In your add-in, in the VBE, use Tools > References to create a reference
to the template. Then call the individual code in the template(s).
- In your add-in use Application.Run to call individual Subs in the
template(s).

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 

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