running a macro

C

Chip Orange

We have been given the task of very quickly converting our organization from
wordperfect to ms word 2002.

One of our needs is to be able to run a macro against any document created
with any template, and to be able to trigger this run via a batch file.

I've tried placing a template to hold such macros in the shared workgroup
templates dir, and then specifying the document name, the template name, and
the macro name on the command line like so:

WINWORD mydoc.doc /Tlibrary.dot /Mmymacro

But while Word does open with the document, no macro is run and no error
message as to why is given.

Can anyone suggest how I would best go about doing this?

thanks.

Chip Orange
 
M

Malcolm Smith

I wouldn't do this in Word. I would write a VB application which opens up
Word, loads the document and then runs the code from the VB application
and not from Word.

- Malc
www.dragondrop.com
 
C

Chip Orange

thanks, but not possible in my situation (which is being driven by a
dos-based database program). At one point, the dos-based database program
needs to open a word document and run a macro against it.

I've placed a global template containing the macro in the startup folder,
but I am still unable to get it to execute with the "winword" command line
which specifies a document and the macro name.

Is it possible that the macro is running before the document loads?
something else entirely?

I've tried both with and without specifying the /t parameter.

thanks.

Chip
 
J

Jay Freedman

Hi, Chip,

The /m switch works *only* with macros in Normal.dot (stupid restriction,
but I think it has to do with the loading order).

A really kludgy method that will work is to create an AutoExec routine in
the global template, and have it call your macro through the OnTime function
(see http://www.mvps.org/word/FAQs/Userforms/AutoExecUsertFm.htm). You don't
need any command-line switches.

The one thing to watch out for is that if there are more than one AutoExec
in different templates (including Normal.dot), they'll all execute. I think
Normal's AutoExec fires first, but I'm not sure whether the order of any
others from the Startup folder is predictable.
 
M

Mark Baird

If what you are trying to accomplish is to run a
particular macro anytime a document is opened then you can
use application events. In this case you would use
Word's "DocumentOpen" event.

Search for "DocumentOpen" in VBA help. This page will tell
you how to use the application events.

You can create this event in a global addin and place that
addin in Word's "Startup" directory.

Mark Baird
 
C

Chip Orange

thanks very much for this *key* piece of information!!

I'm not quite sure however why I can't just execute my macro directly via
the autoexec mechanism you mention, rather than having to use ontime? Maybe
the article you mention will make this clear, but I don't have access to
that web site at the moment.

Chip
 
C

Chip Orange

Thanks, but no, I only want this macro to be executed under very specific
circumstances. It's just that using automation to start it executing isn't
an option for me, so I was trying to use the ms word commandline switch /m.

Since this macro is not part of normal.dot, that appears to be out of the
question (see answer from previous poster).

Chip
 
C

Chip Orange

I meant to ask, if the /m switch only works with macros in normal.dot,
what's the point of the /t<template file> switch?

thanks.

Chip
 
J

Jay Freedman

Hi, Chip,

Try it by just naming your macro AutoExec, and see if it works. You *may*
find that the macro tries to fire before the document loads, causing an
error. If that happens, then use OnTime to delay execution for a second or
two. If everything goes well the first time, so much the better. :)
 
J

Jay Freedman

The /t switch without any other parameters will start Word with a new blank
document based on <template file>. It has nothing to do with macros in
<template file>.
 

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