Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / January 2006

Tip: Looking for answers? Try searching our database.

Macro question: Running Word and macro in batch?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rhino - 27 Jan 2006 15:56 GMT
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)

Signature

Rhino

Helmut Weber - 27 Jan 2006 22:07 GMT
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.

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Rhino - 27 Jan 2006 23:10 GMT
> Hi Rhino,
>
[quoted text clipped - 7 lines]
> and have the macro "test00001" e.g.
> read a txt-file with all the information you need.

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
Helmut Weber - 27 Jan 2006 23:39 GMT
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.

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Jay Freedman - 28 Jan 2006 00:23 GMT
>> Hi Rhino,
>>
[quoted text clipped - 22 lines]
>
>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        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
Steve Yandl - 28 Jan 2006 00:50 GMT
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

>> Hi Rhino,
>>
[quoted text clipped - 22 lines]
>
> Rhino
Rhino - 28 Jan 2006 22:11 GMT
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!

--
Rhino

> Rhino,
>
[quoted text clipped - 67 lines]
>>
>> Rhino
Steve Yandl - 28 Jan 2006 22:40 GMT
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

> Wow, I really like the approach you're suggesting!
>
[quoted text clipped - 78 lines]
>>>
>>> Rhino
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.