How to copy macro from template into document programaticaly?

A

avkokin

There is one template. By this template I'm creating new document. I
need to copy one macro from this template into this document into
module ThisDocument. How?
Thank you.
 
F

Fumei2 via OfficeKB.com

Just one macro? There are others that you do NOT want copied over? You do
know, I assume, that procedures in a template can be used in a document
cloned from that template - as long as the .DOT file is available.

To answer your question though, you could put that procedure into a exported
file (as a separate module perhaps), and then as part of your template
Document_New(), import that file into the newly cloned document VBA project.

So say you put your macro into a module - Yadda. Export it as Yadda.bas
(right click the module to export it). Now write the following into the
Document_New event of the template.

Private Sub Document_New()
ActiveDocument.VBProject.VBComponents.Import "c:\zzz\Yadda.bas"

End Sub

with of course the appropriate path to the file. Now the DOCUMENT, cloned
from the template, will have the imported code module.

Note that you can import any module you like, it does not have to be from the
template cloning itself. So you could, if you like, export some module from
a different template, and import it into a document. Whatever.

Gerry
 
F

Fumei2 via OfficeKB.com

Oops. I missed the part about getting it into ThisDocument. Do yo uhave to
have it in ThisDocument? If so, then the only way is to actually write, line-
by-line, a procedure into ThisDocument.
 

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