How to get Macros to follow into new document created from a templ

K

Katie

I haev a template which is a Word form with several activeX controls in it.
The controls haev VBA attached to them (ie: to fill in the values of a
cascading set of combo boxes).

When I open a new document from the template, the VBA is there and works.
When I save the new document, then re-open it (it is now a *.doc, not a
*.dot), the VBA is no longer there, so the combo boxes do not work.

I know that VBA will stay with the template by default (and is not copied to
the new document). Is there a way to force Word to bring the macros to the
document based on the template? Does anyone have any other suggestions for
getting this to work?

This is for a client, and they are pretty adamant that they want to start
with a template.
 
J

Jezebel

Provided the template is on the machine that uses the form, the VBA code is
still available to the document and should function as normal -- the
relationship between the document and its template is the same whether the
document is newly created or subsequently re-opened. You need to make sure
that everyone using the form has a copy of the template. (An alternative is
to put the code in an add-in, but that's the same distribution challenge.)

While it is theoretically possible to have the template copy code into a new
document, you really do not want to go there: it is technically very
difficult -- in general, making changes in VBA resets the VBA project, which
kills any running code; and it raises unpleasant security issues -- Word is
normally configured to block such actions because that's precisely how some
Word viruses work.
 
K

Katie

Thanks for answering.

The problem is that the code is in the class module for the sheet, and is
event driven, (one procedure to populate a second combo box is driven by the
on change event for the first combo box). So, since the code does not move
to the new file, the new combo boxes have no code attached to them.

If they were public macros, it would not be a problem, since they could be
run manually, but that is not what the client wants.

Katie
 
J

Jezebel

There's not normally a problem attaching a drop-down in a form to code in a
template, or using events in the template's class modules. The template
knows about its documents and can create WithEvents references to them.

No amount of insistence on the client's part will turn an impractical
request into a reasonable one.
 
J

Jonathan West

Katie said:
Thanks for answering.

The problem is that the code is in the class module for the sheet, and is
event driven, (one procedure to populate a second combo box is driven by
the
on change event for the first combo box). So, since the code does not
move
to the new file, the new combo boxes have no code attached to them.

If they were public macros, it would not be a problem, since they could be
run manually, but that is not what the client wants.

I think you need to take a step back here. Forget for a moment about
implementation details. Describe briefly the overall effect you are trying
to achieve. What is supposed to happen to what whom when using your
application?

It may be that there is an entirely different approach that will work better
for you.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
K

Katie

I think I found the problem. Testing it now.

The document_open procedure that populated the first combo box was in a
public module but the client had created it as a private sub instead of a
public sub.

I changed private to public and the dropdown box on the dependent file
(created from the template) happily populated the first dropdown box, and
that even populated the next one, even though the code for that was in a
class module.

Is there anything I should be concerned at when switching private to public
for that procedure?

Thanks

Katie
 

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