Passing a Data File Name via Command Line to a Macro

by Allen Wyatt
(last updated April 19, 2014)

3

Grace is creating a Word macro that performs a mail merge. She wants to start Word from the command line and have it run this macro, which she knows how to do. What she is trying to figure out is how, on the same command line, to pass a data file name that can be accessed by the macro for subsequent use.

A command entered on the operating system's command line can include parameters; this happens all the time. Those parameters can then be accessed by the coding of the command being executed. The problem is that when you start Word on the command line, any parameters are used (or attempted to be used) by Word itself. You need a way for Word to recognize that those parameters are for a macro and then pass them on to the macro. This "recognize and pass on" behavior is not one that is built into Word.

That being said, there are some things you can do to get around this limitation, but the all involve working outside of Word, at a Windows API level. This is pretty advanced stuff, and some suggestions for how to go about it can be found at these sites:

http://vb.mvps.org/samples/CmdLine/
http://msdn.microsoft.com/en-us/library/ms178843.aspx

A simpler approach might be to simply make some changes to the macro that runs when Word is started. The macro could, for instance, display an inputbox to request the user to enter a file name. Similarly, the macro could display an Open dialog box and allow the user to select the file to be processed. Finally, if the file name is standardized (meaning it is always the same and doesn't change), you could directly code the file name into the macro.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (13068) applies to Microsoft Word 2007, 2010, and 2013.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Highlight Words from a Word List

Do you need to highlight certain words in a document, and aren't quite sure how to go about it? Using the techniques ...

Discover More

Making Fast Work of Uploading Files to Drive

What's the fastest way to get files into your Drive space? Why, dragging and dropping, of course! You'll find the complete ...

Discover More

Two-Line Headings in a TOC

If you use the TC field to mark what goes in a TOC, you may wonder why if you mark two lines together with the field they ...

Discover More

Do More in Less Time! Are you ready to harness the full power of Word 2013 to create professional documents? In this comprehensive guide you'll learn the skills and techniques for efficiently building the documents you need for your professional and your personal life. Check out Word 2013 In Depth today!

More WordTips (ribbon)

Repaginating Your Document in a Macro

When processing a document with a macro, you may need to have the macro repaginate the text. It's easy to do using the ...

Discover More

Occurrences of a Text String within a Document

You may have a need to find out how many times a certain text string occurs within a document. You can find out manually ...

Discover More

Moving to the Start or End of the Real Document

The main body of your text is only one part of what makes up the entire document. Documents can consist of other elements, as ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is eight minus 2?

2014-04-23 19:03:19

CMatz

Question: How to handle embedded spaces in the passed filename
Answer: Enclose it with double quotes and then strip them off in the macro
Question: How to handle passing more than one variable
Answer: Same technique, enclose each in double quotes, write them as multiple lines in the temporary text file.
An example for passing two variables is as follows:

---------------------------------SNIP------------------------------------------

@echo off
:Batch File SaveName2.bat
:Example to pass two parameters on command line
:CA Matz - Author
:Call from a command line as: SaveName2 "var1" "var2"
:Where var1 and var2 are variables to be passed to macro and can have embedded spaces
:Writes the variables passed to a temp file "---TMP---.txt" in the Users TEMP directory

ECHO %1 > %temp%---TMP---.txt
ECHO %2 >> %temp%---TMP---.txt


:Start Word with Macro ReadTemp
"C:Program Files (x86)Microsoft OfficeOffice14winword.exe" /MReadTemp2

---------------------------------SNIP------------------------------------------


Sub ReadTemp2()
'CA Matz - Author
Dim hNum As Integer
Dim strFile As String
Dim strLine1 As String
Dim strLine2 As String
Dim DQ As String
DQ = Chr(34)

hNum = FreeFile()

strFile = Environ("temp") & "---TMP---.txt"
Open strFile For Input As #hNum

Line Input #hNum, strLine1
' Strip the Double Quotes is they were passed for the pathfilename
strLine1 = Replace(strLine1, DQ, "")
MsgBox strLine1
Line Input #hNum, strLine2
' Strip the Double Quotes is they were passed for the pathfilename
strLine2 = Replace(strLine2, DQ, "")
MsgBox strLine2

' Your processing using variables strLine1 and strLine2 starts here instead of the message boxes

End Sub


2014-04-22 05:24:19

Emperorbilius

I use a macro to read information from a text file to populate fields in my Word document (version of document, owner, date of issue etc.). The text file is dynamically created by another program which writes the information to my text file.
I use the " GetPrivateProfileString" command to read the information into my Word document and the macro formats it.


2014-04-19 22:00:19

CMatz

Also sent the following as an email due to the text wrapping mess

Write the filename to a temp file with a batch file and then read the temp file within the Word macro to pass the variable

Batch file to save the filename to a temp file

---------------------------------SNIP------------------------------------------
@echo off
:Batch File SaveName.bat
:CA Matz - Author
:Call from a command line as: SaveName argument
:Where argument is the "pathfilename" to be passed to macro
:Writes the argument passed to a temp file "---TMP---.txt" in the Users TEMP directory

ECHO %1 > %temp%---TMP---.txt

: Note! for the drag and drop graphical batch processor FileViking use the following line instead
: ECHO %FV_FILE% > %temp%---TMP---.txt
: FileViking is found at FileViking.com

:Start Word with Macro ReadTemp
"C:Program Files (x86)Microsoft OfficeOffice14winword.exe" /MReadTemp

---------------------------------SNIP------------------------------------------

Word Subroutine to read the temp file into a variable called strLine

---------------------------------SNIP------------------------------------------

Sub ReadTemp()
'CA Matz - Author
Dim hNum As Integer
Dim strFile As String
Dim strLine As String

hNum = FreeFile()
strFile = Environ("temp") & "---TMP---.txt"
Open strFile For Input As #hNum

Line Input #hNum, strLine
MsgBox strLine
' Your processing starts here instead of the message box

End Sub

---------------------------------SNIP------------------------------------------


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.