Passing a Data File Name via Command Line to a Macro

by Allen Wyatt
(last updated March 14, 2020)

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 they 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.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

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, 2013, 2016, 2019, and Word in Office 365.

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

Specifying the Exact Pages to Print

If you have multiple sections in your document, it can be frustrating to print out a limited selection of pages from that ...

Discover More

Sending Drawing Objects to the Back or Front

Not only can you place drawing objects in your worksheets, but you can organize those objects so some are in front and ...

Discover More

Repeating Actions

Need to repeat an action a whole bunch of times? You can do it a time or two using keyboard shortcuts, but you'll need a ...

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)

Accessing the Dirty Flag

When creating macros, you might want to know if a user has made changes in the document or not. Here's how to figure that ...

Discover More

Determining the Hour of the Day

Need to figure out the hour represented by a particular time value? It's easy to do in a macro; just use the Hour ...

Discover More

Adding Automatic Time Stamps

Your computer knows the current date and time, and Word provides ways you can get that date and time into your document. ...

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 five more than 5?

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.