Macro to import macros?

A

ade

I've got a program that creates macros and then writes them to a text
file (or .bas, doesn't matter). I also want to be able to import these
macros into Word and then run them automatically. Since the macros are
modified every few months, the user has to be able to just open word,
import the new macro and overwrite the old one, then run it without
having to go into the VB editor. Is there a way I can do this?
 
J

Jezebel

It's do-able, but fairly complex. Read help (such as it is) and experiment
with the application's VBE property. This gives you access to everything you
see in the VB editor window. You can get to (eg) the NewMacros module using

application.VBE.ActiveVBProject.VBComponents("NewMacros")

The component has methods for adding and removing code.


But this is the hard way to do it, and I think you'll find that a) it's not
very reliable and b) you'll end up with a lot of service calls and
complaints. In my experience, the moment you distribute this sort of code,
you get the blame for EVERYTHING that goes wrong with the users' Word
installation. ("Hey ever since you sent out the new macro, my Bold button
doesn't work...")

Much simpler is to put your macros into a separate template and install that
as an add-in. Then to update your macros you simply distribute a new copy of
the add-in.
 
J

JGM

Hi Jezebel,

How about monkeying around with:

Application.OrganizerCopy Source:="C:\Normal.dot", _
Destination:="Document1", Name:="MyModule", Object:= _
wdOrganizerObjectProjectItems

or something like that? I guess a macro could be written to update the
template's macros from a macro library...

Just asking because I have never used it in a VBA project, I know it exists,
but I thought I'd asked in case you have tried it before.

Thanks.

--
_______________________________________
Jean-Guy Marcil
(e-mail address removed)

Jezebel said:
It's do-able, but fairly complex. Read help (such as it is) and experiment
with the application's VBE property. This gives you access to everything you
see in the VB editor window. You can get to (eg) the NewMacros module using

application.VBE.ActiveVBProject.VBComponents("NewMacros")

The component has methods for adding and removing code.


But this is the hard way to do it, and I think you'll find that a) it's not
very reliable and b) you'll end up with a lot of service calls and
complaints. In my experience, the moment you distribute this sort of code,
you get the blame for EVERYTHING that goes wrong with the users' Word
installation. ("Hey ever since you sent out the new macro, my Bold button
doesn't work...")

Much simpler is to put your macros into a separate template and install that
as an add-in. Then to update your macros you simply distribute a new copy of
the add-in.
 
J

Jezebel

Yes, that's another possibility. There are two problems, though. First, it's
bad practice to make changes to other user's normal.dot (and it's not a good
place to be storing permanent macros anyway); and second, making changes to
the VBA environment resets the VBA projects that are running. So if the user
has code that runs at start-up (eg an AutoExec or AutoOpen function) and
that code sets module-level variables or references, those variables and
references may (will? -- it happens at least sometimes, perhaps always) get
cleared. The user won't know directly that this has happened, they'll just
see that things have stopped working.

With this sort of task, you need to be very careful that you don't interfere
with any *other* macro or add-in code that the user might have installed.
You make yourself unpopular if installing your macro (eg) stops the Acrobat
add-in from working.

JGM said:
Hi Jezebel,

How about monkeying around with:

Application.OrganizerCopy Source:="C:\Normal.dot", _
Destination:="Document1", Name:="MyModule", Object:= _
wdOrganizerObjectProjectItems

or something like that? I guess a macro could be written to update the
template's macros from a macro library...

Just asking because I have never used it in a VBA project, I know it exists,
but I thought I'd asked in case you have tried it before.

Thanks.

--
_______________________________________
Jean-Guy Marcil
(e-mail address removed)

Jezebel said:
It's do-able, but fairly complex. Read help (such as it is) and experiment
with the application's VBE property. This gives you access to everything you
see in the VB editor window. You can get to (eg) the NewMacros module using

application.VBE.ActiveVBProject.VBComponents("NewMacros")

The component has methods for adding and removing code.


But this is the hard way to do it, and I think you'll find that a) it's not
very reliable and b) you'll end up with a lot of service calls and
complaints. In my experience, the moment you distribute this sort of code,
you get the blame for EVERYTHING that goes wrong with the users' Word
installation. ("Hey ever since you sent out the new macro, my Bold button
doesn't work...")

Much simpler is to put your macros into a separate template and install that
as an add-in. Then to update your macros you simply distribute a new
copy
 
L

Lars-Eric Gisslén

Ade,

We have a global template that creates several macros and forms by code and
also imports modules from files into new documents, but that's one way,
never replacing macros.

The global template has over 1 meg of macros and each time it's loaded it
checks if there is any newer version of it on the server. If the macro finds
a newer version of the template it pops up a message telling the user that a
new version of the template is available. The user can the choose to update
the template from a menu. The macro, which is also in the global template,
downloads a document with macros for replacing it on the client machine.

It may seems to be a little but clumsy to open a new document that in turn
will update the global template. There are two resons for this. For the
first, we don't want to have this code in normal.dot because we always keeps
it clean from macros, for known reasons. For the second, we can't have the
code in the global template as the dot file has to be replaced with a new
file and you can't overwrite a dot file as long as macros are executing in
it. So, the macros in document detach the global template from Word and
download the new template from the server and attach the template to Word
again so it will be available at ones with no need for the user to restart
Word. This has been working without any problems for about two years now.

Regards,
Lars-Eric
 

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