Macro question: Running Word and macro in batch?

R

Rhino

I am trying to create a Word document by opening an existing document from
the command line and running a macro that erases the current version of the
document and regenerates it from an external file. Is it possible to do
that? Ideally, I'd like to do this without even opening Word, just running
it in batch. If this is possible, how would I do that?

Also, it is possible to pass one or more parameters to the macro from the
command line: If so, what would the syntax be? I'm guessing something like
this:

Word -mode=batch -run=macro01(parameter1, parameter2)
 
H

Helmut Weber

Hi Rhino,

I've read some stuff about getting the commandline.
Complicated and unreliable, as it seems.

Start Word like this:

c:\prg\mso2003\office11\winword.exe /mtest00001

and have the macro "test00001" e.g.
read a txt-file with all the information you need.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
R

Rhino

Helmut Weber said:
Hi Rhino,

I've read some stuff about getting the commandline.
Complicated and unreliable, as it seems.

Start Word like this:

c:\prg\mso2003\office11\winword.exe /mtest00001

and have the macro "test00001" e.g.
read a txt-file with all the information you need.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"

Excellent, that works!

Now, do you have any idea how to pass a parameter to the macro on the
command line? Something like:

c:\prg\mso2003\office11\winword.exe /mtest00001 Arial

if you wanted to compose the document using the Arial font family?

(I'm not actually trying to direct the macro which font family to use; I
really just need to know the principle of how to pass a parameter to the
macro and how to get the macro to use it.)

Rhino
 
H

Helmut Weber

Hi Rhino,
Now, do you have any idea
how to pass a parameter to the macro on the command line?

no.

Somebody else may know better.

Anyway, the number of the command line arguments would be limited.
In theory,
only the operating system and the hardware set the boarders.

With reading from a txt-file or any other file,
the possibilities would be _almost_ unlimited.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
J

Jay Freedman

Excellent, that works!

Now, do you have any idea how to pass a parameter to the macro on the
command line? Something like:

c:\prg\mso2003\office11\winword.exe /mtest00001 Arial

if you wanted to compose the document using the Arial font family?

(I'm not actually trying to direct the macro which font family to use; I
really just need to know the principle of how to pass a parameter to the
macro and how to get the macro to use it.)

Rhino

Hi Rhino,

No, there's no facility for passing parameters on the command line.
(The full story on command-line switches is at
http://support.microsoft.com/?kbid=210565.) That's why Helmut told you
to have the macro read its information from a text file or other kind
of file (whose path and name must be constant because you can't pass
that on the command line either). An alternative is to have the
information stored in a known place in the registry.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
S

Steve Yandl

Rhino,

If I were you, I'd use vbScript and take advantage of the fact that the
Windows Script Host has an arguments collection. In the example below, I
have a Word document named TestDoc.doc that resides in the MyDocuments
folder and contains a macro named MyMacro that takes two arguments as text
strings. If I make a text file with the code below and name it with a vbs
extension, I can launch with my arguments as follows:
C:\Test\LaunchWdMacro.vbs StringA StringB
Word runs but isn't visible. The macro runs and takes the two arguments.

Code below.

' Get arguments into variables
If WScript.Arguments.Count < 2 Then
MsgBox "Not enough arguments"
WScript.Quit
Else
strArg1 = WScript.Arguments.Item(0)
strArg2 = WScript.Arguments.Item(1)
End If

' Find path for MyDocuments folder
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(&H5&)
strMyDocPath = objFolder.Self.Path

' Start Word Application, open TestDoc.doc in MyDocuments
Set oWd = CreateObject("Word.Application")
oWd.Visible = False
Set oDoc = oWd.Documents.Open(strMyDocPath & "\TestDoc.doc")

' Run macro named MyMacro with two string arguments
oWd.Run "MyMacro",strArg1,strArg2

' Save changes to doc on closing and quit Word
oDoc.Save
oDoc.Close
oWd.Quit
Set oWd = Nothing

Set objShell = Nothing


Steve
 
R

Rhino

Wow, I really like the approach you're suggesting!

I've tried your example and it worked fine so I think I will use it for my
project. However, I will keep the other suggestions in mind as a backup, in
case I start running into unexpected problems with your approach.

Thanks very much for the suggestion!
 
S

Steve Yandl

You're welcome.

I'd probably create an instance of the file system object,
Set objFSO = CreateObject("Scripting.FileSystemObject")
and use it to check for files and folders or to delete if that is what you
want. It sounded like you already have that written into your macro and I
didn't want to confuse matters by duplication of effort but you might find
it to be a workable option.

I still use batch files from time to time but vbScript gives you so much
more in the area of controlling objects (the different Office applications
for example) that I've pretty much converted to vbs for this type project.

Steve Yandl
 

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